I want the variable inputname to go up by 1 every time a new <input /> is added
e.g.
<input name="1" />
<input name="2" />
<input name="3" />
—html—
<p class="add">Add</p>
<div class="added"></div>
—jQuery/javascript—
$(document).ready(function() {
$("p.add").click(function() {
var inputname = somevar;
var added = "<input type=\"text\" name=\""+inputname+"\" />";
$("div.added").append(added);
});
});
here it is on jsfiddle.net if it helps -> http://jsfiddle.net/gamepreneur/54kzw/
Change the variable scope
Use this:
Alternatives:
$('div.added input').length) and use that for a counter.$('div.added input:last').prop('name')) and increment it.