How can I select item with class within a DIV?

Idan Shechter picture Idan Shechter · Aug 3, 2011 · Viewed 380.5k times · Source

I have the following HTML:

<div id="mydiv">
  <div class="myclass"></div>
</div>

I want to be able to use a selector that selects the inside div, but specific for the mydiv container. How can I achieve this with jQuery?

Answer

Try:

$('#mydiv').find('.myclass');

JS Fiddle demo.

Or:

$('.myclass','#mydiv');

JS Fiddle demo.

Or:

$('#mydiv .myclass');

JS Fiddle demo.

References:


Good to learn from the find() documentation:

The .find() and .children() methods are similar, except that the latter only travels a single level down the DOM tree.