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; });
set elements id using .each()
$(':checkbox').each(function( ){ this.id = i; });
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
Post a Comment