javascript - Increment elements ID -


but code checkbox elements have same id...

var count; var length = $('input[type=checkbox]').length for(count=1;count<=length;count++){     $('input[type=checkbox]').attr('id',count) } 

set elements id using .prop() or .attr()

$(':checkbox').prop("id", function( ){     return i; }); 

jsbin demo

set elements id using .each()

$(':checkbox').each(function( ){     this.id = i; }); 

jsbin demo

both examples return:

<input id="0" type="checkbox"> <input id="1" type="checkbox"> <input id="2" type="checkbox"> <input id="3" type="checkbox"> 

if want start 1 use:

this.id = i+1; 

since numerical id not supported in non html5 (older) browsers, add string prefix id number "el"+ (i+1)


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -