I have form like this :
<div id='add_field' class='locbutton'><a href='#' title='Add'>Add</a></div>
<div id='remove_field' class='locbutton2'><a href='#' title='Delete'>Delete</a></div>
<div id='idisikpi'>
<div id='idheadisi1' class='headisi'>
<div class='performancedetail'><input type='text' name='idperformancedetail[1][]'
value='2' id='idperformancedetail[1][]'></div>
<div class='performancedetail'><input type='text' name='idperformancedetail[2][]'
value='9' id='idperformancedetail[2][]'></div>
</div>
</div>
This is the jquery script :
function CreateInput()
{
var $headisi = $("<div>"),
$idperformancedetail=$("<div>");
var headisi_count = $("div.headisi").length+1;
var idperformancedetail_count =
$("[id^='idperformancedetail']").last().val().length+1;
$headisi.addClass("headisi");
$idperformancedetail.addClass("performancedetail");
var $input_idperformancedetail = $("<input>")
$input_idperformancedetail
.attr("name", "idperformancedetail["+headisi_count+"][]")
.attr("type", "text")
.attr("id", "idperformancedetail["+headisi_count+"][]")
.attr("value", idperformancedetail_count);
$idperformancedetail.append($input_idperformancedetail);
$headisi.append($idperformancedetail);
return $headisi;
}
$("#add_field a").click(function(e)
{
$("#idisikpi").append(CreateInput());
e.preventDefault();
});
$("#remove_field a").click(function(e){
$("#idisikpi div.headisi:last-child").remove();
CalculateTotal();
e.preventDefault();
});
I want the next input is counter from last value of idperformancedetail, i want value on the next input like 10,11,12,…What can i do to get counter value on the next input ? Please Help me 🙁
You have to parse
$("[id^='idperformancedetail']").last().val()as an integer (parseInt), otherwise you’ll get a concatenated string. Also you don’t want to use.length, butval():JSFiddle demonstration. Note that
val()will only work since you set thevalueattribute in thedivyourself. This will completely ignore yourinput‘s value.