0

I've looked at other questions, but none of them have really helped me. I want to give IDs to a number of elements belonging to the same class without manually doing it. Here is my code that isn't working:

$curelem = $(".item:first");

for (var $i=0; i < $(".item").length; ++$i){
    $curelem.attr("id", "item" + $i);
    $curelem = $curelem.next('a');
}

Is it some small syntax error, or am I going about it entirely wrong?

3
  • 1
    May I ask why? IDs with numbers are ---usually--- code smell. Commented Jan 25, 2014 at 22:41
  • In my HTML I've got a sidebar with <a>s in it, and when I click on one of these, I want to be able to show a specific div. That's kind of obscure, I realize Commented Jan 25, 2014 at 22:54
  • So, the reason you're using IDs like that is that you want to bind these div elements to clicking on the as? Why not use an explicit binding in your code? Alternatively, a worse but simpler and still better solution, you can store a data-url attribute on your a tags that contains a selector for the div elements you want to open. Commented Jan 25, 2014 at 22:56

4 Answers 4

1

If you're using jQuery you can use jQuery's each() function:

var i = 0;

$('.item').each(function(){
   $(this).attr('id', 'item' + i);
   i++;
});
Sign up to request clarification or add additional context in comments.

13 Comments

this.id = 'item' + i; for one. Though I'm afraid the underlying issue here is much bigger. Sequential numeric IDs are the indication of bad design - usually they should be an array instead.
@jd182 the problem OP is having is the XY problem he thinks he wants to select all elements by class and add them numeric IDs where in practice what he really wants is to either use .eq with an index or better yet - use arrays. Selectors (jquery and others) are only useful at the initialization phase of the application. Once you have everything in place there is no reason to query your own presentation. OP can assign the elements of the class into an array.
@brooks.johnson if you're using jQuery you should not have to use getElementById anyway. You can use $("#"+id) - but like I said - this is the XY problem - you can just keep the selection and then access it like an array: var items = $(".item") and then access elements through var cur = items[2]; //the third item which has the same type as what you get from getElementById anyway.
@BenjaminGruenbaum I think this definitely is a case of the XY problem. I've decided not to assign IDs like this now, but all of this has helped me figure out different ways to do what I want. Thanks!
@FernandoSilva for that I'd need OP's actual HTML. The problem with sequential IDs builds up to "How do I structure UI in web development" which is way beyond the scope of an SO answer :D
|
1
$('.item').each(function (i, el) {
    el.id = "item" + i;
});

Comments

0
$('.item').each(function(i) {
    this.setAttribute('id', "item" + i);
});

1 Comment

How is this answer any different from Andrew's from 5 minutes ago. Or Pat's from 6? Or jd182 from 8?
-1

Use http://api.jquery.com/jquery.each/ to loop through elements:

$(".item").each(function( index, value ) {
  $(this).attr("id", "item"+index);
});

1 Comment

This is just like the two other answers except it is incorrect.

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.