jQuery search element

Is there a way to do this in jQuery?

const outer = document.querySelector('.outer');
const inner = outer.querySelector('.inner');

Sure :slight_smile:

const $outer = $('.outer').first();
const $inner = $outer.find('.inner').first();

Of course the above will return two jQuery objects as opposed to DOM node references. If you want the node reference, just grab the first element in the collection:

const $outer = $('.outer').first();
console.log($outer);
console.log($outer[0]);

// Object { 0: div.outer, length: 1, prevObject: {…} }
// <div class="outer">

Out of interest, why do you want to do that? document.querySelector is supported in all modern browsers and even in IE11.

1 Like

Oh, thanks! One of the plugins I use doesn’t support vanilla js and only jquery. :man_shrugging: :man_shrugging:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.