Let's say that you iterate through some DOM objects and you want to find and catch an element with a specific identifier
<div id="myDiv"> <div id="fo"><div> <div id="bar"><div> </div>
You can write something like search
$('#myDiv').find('#bar')
Note that if you must use the class selector, the find method will return all matching elements.
or you can write an iteration function that will do more complex work.
<div id="myDiv"> <div id="fo"><div> <div id="bar"><div> <div id="fo1"><div> <div id="bar1"><div> <div id="fo2"><div> <div id="bar2"><div> </div> $('#myDiv div').each(function() { if($(this).attr('id') == 'bar1') //do something with bar1 });
The same code can be easily changed for the class selector.
<div id="myDiv"> <div class="fo"><div> <div class="bar"><div> <div class="fo"><div> <div class="bar"><div> <div class="fo"><div> <div class="bar"><div> </div> $('#myDiv div').each(function() { if($(this).hasClass('bar'))
I'm glad you solved your problem with index (), which ever works for you. I hope this helps others with the same problem. Greetings :)
Nima Foladi Jan 05 '12 at 13:30 2012-01-05 13:30
source share