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

Winapi c++: DialogBox hangs when breaking a loop -

vb.net - Font adding using PDFsharp -

javascript - jQuery iScroll clickable list elements while retaining scroll? -