Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Return different for element .getFirst() vs .getLast() #2394

Closed
jbgithub opened this Issue Aug 6, 2012 · 4 comments

Comments

Projects
None yet
3 participants

jbgithub commented Aug 6, 2012

I am using Element.getLast() and Element.getLast() on a TBODY element; getLast() returns the last element (type DispHTMLTableRow) but getFirst() returns a collection of cells (type Object) for the first row.

Based on the documentation I would think they both returned an element.

This issue was mentioned in pull request 78 one year ago but I have not seen an issue opened for this.

Member

kassens commented Aug 7, 2012

Can you create a test case on http://jsfiddle.net/ ? I couldn't reproduce your issue.

jbgithub commented Aug 7, 2012

I entered a test case in jsfiddle but I am unfamilar with how you get to see it.

This is the URL for the test case: http://jsfiddle.net/24HJD/

Owner

arian commented Aug 7, 2012

This is not a bug.

See this jsfiddle: http://jsfiddle.net/24HJD/1/

What .getChildren() does is return an collection, an instance of Elements. Elements has also all Element methods, including getFirst(). However Array also has a getLast method, no getFirst() method because that's always array[0]. What happens is that Elements has Element.getFirst() but Array.getLast().

So the line with getFirst() it gets the first element of each element in the collection. getLast returns the last item of the collection. A collection doesn't have a id property of course, while the last element does have one.

Hope this clears it up a bit. It is a bit inconsistent in this specific case, but on the whole it is better that array methods take precedence over Element methods.

@arian arian closed this Aug 7, 2012

jbgithub commented Aug 7, 2012

After I posted the test case I realized what was wrong; I shouldn’t have used getChildren().

I didn’t see it earlier because I was adding getFirst() code to old code that incorrectly used getLast() with getChildren(). Since getLast() worked I thought getFirst() would work the same way too.

This is the corrected code:
var oTBody1 = $("tbBody1");
var oFirstRow = oTBody1.getFirst();
var oLastRow = oTBody1.getLast();

Thanks for the explanation why getLast() worked and getFirst() did not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment