[jquery] Find an element by class name, from a known parent element

I want to find an element by class name. I know it will appear in a particular parent div, so rather than search the entire dom, I'd like to search only the particular div. I am trying this, but does not seem to be the correct syntax:

var element = $("#parentDiv").(".myClassNameOfInterest");

what's the right way to do that?

Thanks

This question is related to jquery

The answer is


You were close. You can do:

var element = $("#parentDiv").find(".myClassNameOfInterest");

Alternatively, you can do:

var element = $(".myClassNameOfInterest", "#parentDiv");

...which sets the context of the jQuery object to the #parentDiv.

EDIT:

Additionally, it may be faster in some browsers if you do div.myClassNameOfInterest instead of just .myClassNameOfInterest.


var element = $("#parentDiv .myClassNameOfInterest")