getting my head around javascript in adding an auto increment id for a list-item
<script type="text/javascript">
var increments = document.getElementById("increment");
var li = increments.getElementsByTagName("li");
for (i=0;i<li.length;i++) li[i].setAttribute("id", 'x' + i+1);
</script>
the problem is that after the second id of ‘x21’ it seems to drop off and not insert an id for the third, forth li.
maybe its staring at me in the face but i cant see it and my feeble mind is still learning
thnx for having a look
Check this fiddle.
It works ok for me. I changed the way you assign the id and used local variables instead of the global “i”.
Also, better to always use curly brackets even for one line loops and if statements.