Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery select child element by class with unknown path

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?

like image 628
Adam James Avatar asked Aug 02 '13 12:08

Adam James


4 Answers

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.

like image 154
Casey Avatar answered Nov 10 '22 08:11

Casey


$('#thisElement').find('.classToSelect') will find any descendents of #thisElement with class classToSelect.

like image 29
cfs Avatar answered Nov 10 '22 06:11

cfs


This should do the trick:

$('#thisElement').find('.classToSelect')
like image 19
Joe Meyer Avatar answered Nov 10 '22 06:11

Joe Meyer


Try this

$('#thisElement .classToSelect').each(function(i){
         // do stuff
});

Hope it will help

like image 9
Sonu Sindhu Avatar answered Nov 10 '22 07:11

Sonu Sindhu