Search code examples
javascriptjquerymootoolsdollar-sign

jQuery equivalant of mootools $$


I am migrating my code to jQuery from mootools. There is a line of code that uses mootools '$$' to get an array of elements with the given selectors

var myMenu =  $$('#outerDiv .menu');

I have tried jQuery('#outerDiv .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().)


Solution

  • The closest jQuery equivalent to Mootools' $$ would be calling .get() on a jQuery object:

    var myMenu = jQuery('#outerDiv .menu').get();
    

    From jQuery.fn.get docs:

    Without a parameter, .get() returns all of the elements [...]
    All of matched DOM nodes are returned by this call, contained in a standard array.

    When using jQuery though, you usually don't do this very often as the returned elements array won't support any jQuery method. Remember that jQuery handles iteration over DOM collections and methods chaining out of the box.