How can I select item with class within a DIV?

JavascriptJqueryHtmlClassJquery Selectors

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

Try this

$("#mydiv div.myclass")

Solution 3 - Javascript

You'll do it the same way you would apply a css selector. For instanse you can do

$("#mydiv > .myclass")

or

$("#mydiv .myclass")

The last one will match every myclass inside myDiv, including myclass inside myclass.

Solution 4 - Javascript

If you want to select every element that has class attribute "myclass" use

$('#mydiv .myclass');

If you want to select only div elements that has class attribute "myclass" use

$("div#mydiv div.myclass");

find more about jquery selectors refer these http://jascripts.blogspot.com/2015/12/how-to-use-basic-jquery-selectors-part.html">articles</a>

Solution 5 - Javascript

try this instead $(".video-divs.focused"). This works if you are looking for video-divs that are focused.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLiron HarelView Question on Stackoverflow
Solution 1 - JavascriptDavid ThomasView Answer on Stackoverflow
Solution 2 - JavascriptShankarSangoliView Answer on Stackoverflow
Solution 3 - JavascriptLeandro GalluppiView Answer on Stackoverflow
Solution 4 - Javascripte11438View Answer on Stackoverflow
Solution 5 - Javascriptuser3749775View Answer on Stackoverflow