[Solved] How to asign same id’s to the multiple elements via for loop through every 5 element [closed]


As absolutely terrible an idea as this is it can be accomplished via jQuery.

$('div').each(function (i) {
    if (i % 5 == 0) $(this).attr('id','New Id');
});

However, I am assuming you’re doing this to style elements, and this is one place newcomers get screwed up is not understanding that ID’s are UNIQUE elements, their only real purpose to be honest is to allow you to uniquely select them. To style things similarly on the same page you want to use classes, not ID’s. In that case selecting and changing every 5th element to the same CLASS is perfectly valid and a normal thing to do.

$('div').each(function (i) {
    if (i % 5 == 0) $(this).addClass('newClass');
});

5

solved How to asign same id’s to the multiple elements via for loop through every 5 element [closed]