jQuery select child element by class with unknown path

Adam James picture Adam James · Aug 2, 2013 · Viewed 119.6k times · Source

I am trying to figure out the syntax for selecting a nth-child of an element by its class, however I don't know the exact path to the element. I can't do $('parent > child > grandchild > hereIam');

So basically I need to be able to say

$('#thisElement').AllRelativesWithClass('.classToSelect')

How exactly do I do that?

Answer

Casey picture Casey · Aug 2, 2013

According to this documentation, the find method will search down through the tree of elements until it finds the element in the selector parameters. So $(parentSelector).find(childSelector) is the fastest and most efficient way to do this.