Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery find - What order does it return elements in?

I have been using jQuery's find method and it's been very useful.

However, I came across an example where the find seemed to be duplicating things and not returning elements in the order they appeared in the document. (I'm not sure if find is supposed to do this - I doubt it).

However, it shouldn't have duplicates in the elements it finds and show the wrong order, surely?

Full example can be found here: jsFiddle - Notice how span[9] and span[10] are in the wrong order and duplicated.

Why is this the case?

Update

Updated so that output is written to document, please use new link above.

like image 934
Abs Avatar asked Jan 07 '12 17:01

Abs


People also ask

How to get position of element in jQuery?

jQuery position() Method The position() method returns the position (relative to its parent element) of the first matched element. This method returns an object with 2 properties; the top and left positions in pixels.

What does jQuery find return?

jQuery find() Method The find() method returns descendant elements of the selected element. A descendant is a child, grandchild, great-grandchild, and so on. The DOM tree: This method traverse downwards along descendants of DOM elements, all the way down to the last descendant.

How to check index in jQuery?

The index() method returns the index position of specified elements relative to other specified elements. The elements can be specified by jQuery selectors, or a DOM element. Note: If the element is not found, index() will return -1.

How to find child in jQuery?

The children() method returns all direct children of the selected element. The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return grandchildren or other descendants), use the find() method.


1 Answers

.find() returns elements in document order. More info here: http://docs.jquery.com/Release%3ajQuery_1.3.2

I think the anomaly has something to do with the wildcard selectors. Are those necessary? Removing them seems to resolve the issue.

like image 184
Rob Flaherty Avatar answered Oct 21 '22 16:10

Rob Flaherty