javascript - change attrib name with array -
i have form input:
<input id="cant_user" type="text" name="cant_user" value="1" onblur="addinputs()">
#userdata
cloned when add input value greater 1 , want change input name user_age
age[n]
<div id="userdata"> <div class="panel-body" id="generated[0]"> <div class="form-group"> <label class="col-md-4 control-label">age user [n]</label> <div class="col-md-6"> <input type="number" class="form-control" name="user_age[]" value="0" required> </div> </div> </div>
javascript:
function addinputs() { var cantusers = document.getelementbyid("cant_user").value; var countgenerated = $('[id^=generated]').length; if (countgenerated > 0) { $('[id^=generated]').not(':first').remove(); } if (cantusers != 1) { if (cantusers > 1 && cantusers < 10) { (var = 1; < cantusers; i++) { jquery("#userdata").append(jquery("#userdata").children().first().clone()); jquery("#userdata").children().last().attr("id", "generated[" + + "]"); (var n = 0; n <= i; m++) { var u = document.getelementsbyname("user_age[]"); $(u).removeattr("name").attr({ id: "generatedage[" + n + "]", name: "age[" + n + "]" }); } }; } }; }
how can make every label , input name shows value array like:
<label>age user 1<label> <input name="age[0]"> <label>age user 2<label> <input name="age[1]">
i changed code, maybe can if understand idea correct
for ( var = 1; < cantusers; i++) { var userdata = jquery("#userdata"); var childofuserdata = userdata.children(); userdata.append( childofuserdata.first().clone() ); childofuserdata = userdata.children(); //because appen new data childofuserdata.last().attr("id","generated[" + + "]"); ( var n = 0; n <= i; n++ ) { var child = childofuserdata.get(i); var userage = $("input", $(child));//because have 1 input userage.attr("name","age[" + n + "]").attr("id","generatedage[" + n + "]"); }
wiki
Comments
Post a Comment