I have a large file of this form [similar div's throughout]. I want to be able to select a div
, find the number of ul
's in it and traverse through each of them to get value of each li
in it.
<div class="experiment">
<div class="experiment-number">5</div>
<ul class="data-values">
<li><div></div> 14</li>
<li><div></div> 15</li>
</ul>
<ul class="data-values">
<li><div></div> 16</li>
</ul>
</div>
I have tried looping through all experiment div
s, then select the ul
s, but it selects all the ul
in the page, not only the ones under current div
.
$('experiment ul').eq('$i');
Your HTML is currently incorrect, since you're simply starting new
<div>
and<ul>
elements rather than closing the existing ones. Ignoring that because it's trivial to fix, we'll move on to the real issue.You need to select all of the
<div class="experiment">
elements, then iterate through them. To do that you can use the.each()
function. It might look something like this:Take a look at this jsFiddle demo.
First of all you need to work out the correct selector for each DIV.
The selector you want is:
Notice the
.
to denote a class selector.This will allow you access to each DIV element. If you then want to loop though each of these, you can do so like this:
IMPORTANT: There is also an error with your HTML, you need to close your
<ul>
elements correctly using</ul>
to get all the ul inside div.experiment
and to get all li elements inside each ul found above