html - jquery .each works only on the first element -


i'm having trouble understanding jqueries .each. have following code:

$('#testdiv').each(function(index, domele){         $(this).text(index); }); 

and following html

<div id="p18">     <div class="inner">         <span>...</span>         <p class="right">...</p>         <div id="testdiv"></div>     </div> </div> <div id="p19">     <div class="inner">         <span>...</span>         <p class="right">...</p>         <div id="testdiv"></div>     </div> </div> <div id="p20">     <div class="inner">         <span>...</span>         <p class="right">...</p>         <div id="testdiv"></div>     </div> </div> 

when script runs works first testdiv, correctly sets text 0, other testdivs.

my overall goal write script set height of div based on div's height. heights differ think loop structure way go (unless mistaken?)

what doing incorrectly jq code?

you can't use same #id different elements. try rename rest , you'll result want

or (works without adding classes - cleaner code)

$('.inner div').each(function(index, domele){         $(this).text(index); }); 

Comments

Popular posts from this blog

How do you change vbulletin's home page to the forums instead of the default (CMS or Activity Stream)? -

c++ - Troubles when compiling phash program -