I have a loop that runs through first level LI items of a menu. I want to detect the end of the loop, however, the print out I get seems to run twice.
Here's my JS:
var numNavItems = $("#navigation > li").size() - 1;
$("#navigation > li").each(function(i) {
$(this).delay( i * 300 ).animate({ opacity: 1 }, 300);
// This runs twice
$("#content").append("<p>Loop number: " + i + " out of " + numNavItems + "</p>");
if( i == numNavItems) {
//$("#navigation li").css({ "opacity" : 1 });
//alert("End! Number of items = " + numNavItems + ". Last item = " + i );
}
});
So, my logic fires twice. Not sure why, could it be my nested list?
Here's the HTML...
<ul id="navigation">
<li class="selected"><a href="./">Home</a></li>
<li><a href="./portfolio/">Portfolio</a>
<ul>
<li><a href="#">Cosmos</a></li>
<li><a href="#">Remora</a></li>
<li><a href="#">Caspian</a></li>
<li><a href="#">Megaway</a></li>
</ul>
</li>
<li><a href="./philosophy/">Philosophy</a></li>
<li><a href="./about/">About</a></li>
<li><a href="./contact/">Contact</a></li>
</ul>
I used the ">" to find only the first level LI's.
It prints:
Loop number: 0 out of 4
Loop number: 1 out of 4
Loop number: 2 out of 4
Loop number: 3 out of 4
Loop number: 4 out of 4
Loop number: 0 out of 4
Loop number: 1 out of 4
Loop number: 2 out of 4
Loop number: 3 out of 4
Loop number: 4 out of 4
Many thanks for your help. Michael.