Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the result of jQuery .find() function to an array?

What does jQuery .find() method return? a object OR a array list of objects?

If it returns an object which contain all the matched elements. How to convert this object to an array?

If it returns a array of elements, why $(xml).find("DATE").sort(mySortFunc); does not work, it seems the jQuery .find() returns an object which can not apply Javascript sort() method which is supposed to be applied on array.

Generally, I need to sort the objects find by $(xml).find("DATE") , but when I use sort function, it raised an error that the object can not be resolved.

like image 709
Leem Avatar asked Sep 22 '11 10:09

Leem


People also ask

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.

Does find return jQuery object?

find() returns an object even when there's no matching child element in the DOM.


1 Answers

The majority of jQuery methods returns a jQuery object, which can be accessed like it is an array (e.g. it has a .length attribute, elements can be accessed using the square bracket notation ([0]), and it supports some array methods (slice())).

jQuery has a method called toArray() which can be used to convert the jQuery object to a real array.

You can also use get() with no arguments to achieve the same effect (and save you a few key presses).

In future, you can checkout the jQuery API, and the return type for all jQuery methods is listed in the relevant documentation (e.g. for find(), the return type is "jQuery")

like image 136
Matt Avatar answered Oct 17 '22 23:10

Matt