mootools double dollar equivalent in jquery

1.2k views Asked by At

I have a line of code that uses mootools to get an array of elements with the given selectors

var menuItems =  $$('#container .menu');

I need to convert this to jquery but can't find a solution. I have tried jQuery('#container .menu') but it doesn't return an array.

Is there a way to use '.find()' from jquery on the whole document ? (since I don't have a parent element to make it like parent.find()..)

Any other suggestion is also most welcome

2

There are 2 answers

0
nnnnnn On

With jQuery your statement:

jQuery('#container .menu')

will return a jQuery object that contains all matching elements where you can access the individual DOM elements with array-like syntax:

var menuItems = jQuery('#container .menu');

menuItems.length        // gives a count of how many matched
menuItems[0]            // the first matching element

// but you can also use jQuery methods on the object
menuItems.hide();

If you want an actual array rather than an array-like object you can use jQuery's .toArray() method:

var menutItemsArray = jQuery('#container .menu').toArray();

Why don't you try one of the jQuery tutorials available at the jQuery website?

0
Anthony Grist On

If you absolutely need it as an array, rather than working with the jQuery object returned by the selector, take a look at the .toArray() function.