I want to add progressive id to a series of elements on an unordered HTML list. I used the jQuery .each() method to get each <li> of the List and append a <span> inside each <li>. Then I added the ID attribute with index of each() as number.
$('#menu li ul li').each(function(i, e){
$(this).append('<span class="arr"></span>');
$(".arr").attr("id", "id_" + i);
});
But I get id_3 for all elements. Why? What did I do wrong?
Thanks for any help!
$(this).append('<span class="arr"></span>').attr("id", "id_" + i)or$(this).append('<span class="arr" id="id_'+i+'"></span>')will do what you want ...$(".arr").attr("id", "id_" + i)will set the same i to all arr class