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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -