2

I just have a quick question about how to generate id's on-the-fly for HTML elements. So far I've tried a few things, I started with a "for" loop, I already know how many elements I have to generate Id's for, in this case I have an "ul" with 6 "li". My "for" loop is as follows:

var items = $("ul li").length;
for(var i = 0; i <= items; i++){
    $("ul li").attr("id", "number" + i);
}

"number" would be the new id concatenated with "i", so I get a different Id for each "li". As you can probably tell, this does not work, because I end up with the same Id for each "li":

in this case I get <li id="number6">... </li> for all the "li" elments in the "ul". I tried a "while" loop and ".each()" with jQuery but I get the exact same thing.

Any help would be appreciated.

4
  • There is no reason why you should end up with the same id for each. There are no deferred functions or closures that would cause the same variable to be used as "number" + i is evaluated on each iteration. Commented May 26, 2010 at 21:04
  • I actually was not expecting this to trick me like it did, but for some reason it was not working. It is now thanks to a different approach provided by Nick Craver(below). Thanks Commented May 26, 2010 at 21:10
  • @Sean .attr("id", value); sets it for each of the matched elements, like every other action. It was setting it for every <li> every time, e.g. all were set to number0, then all were set to number1, etc. Just needed to use this instead of selecting and setting all elements each loop...what he had was the expected result, just not right intended one. Commented May 26, 2010 at 21:36
  • @Nick - Thanks again nick, I suspected that was the problem, but then again your advice help me figure it out. Commented May 26, 2010 at 22:31

2 Answers 2

8

You can do this easier using a .each() (use this inside, not the selector again!), like this:

$("ul li").each(function(i) {
  $(this).attr("id", "number" + i);
});

The function in the .each() gets the index and element as a parameter, so you can just use that i to assign the ID starting at 0 like you have currently.

Or, alternatively, you can pass a similar function to .attr(), like this:

$("ul li").attr('id', function(i) {
  return "number" + i;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the "each" method works perfectly as you point out, it did not work for me before, but I must have been doing something wrong, Thanks a lot
1

Your code is pretty off, and I just want to note this in case anyone comes to this thread looking for a similar answer.

var items = $("ul li").length;
for(var i = 0; i <= items; i++){
    $("ul li").attr("id", "number" + i);
}

In your for loop, you're grabbing every "ul li" again on each iteration, and setting the ID's of all of them to be a specific ID. What you actually wanted was the .each() method, to iterate through all of them:

$("ul li").each(function(i) {
    $(this).attr("id", "number" + i);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.