I have form with single text input to which I want to add functionality - on click generate new text input. Here's my code:
HTML
<form>
Fill:
<input type="text" name="0" class="target"/>
<br /><br /><br /><input type="submit" value="post it!" />
</form>
JS/jQuery
<script>
var i=1;
$(function(){
$("input.target:last").on("click", function(){
$("input.target:last").after("<br /> <input type='text' " + "name='" + i +"'" + ' class="target" />');
i++;
});
});
</script>
This works fine but only when clicking on first (non-dynamically) generated input. I've checked in Firebug, generated inputs are just fine - they are generated with correct class name and name attribute are incremented as expected - everything fine except click event is not related to any input but the first one, non- dynamically generated.