<h2><a href="#" id="addScnt">Add Another Input Box</a></h2> <div id="p_scents">
<p id="test">
<input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" />
<input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" />
</p> </div>
$(function() {
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 1;
$('#addScnt').live('click', function() {
$('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
i++;
alert(
$("#test > input").size()
);
return false;
});
$('#remScnt').live('click', function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
if i click then value = 2. this is good. and added new input, but if i again click then value agan = 2, should be = 3. I cant use $i. how can i modified function size(), that checked live?
live example: http://jsfiddle.net/u9uhV/6/
You’re still getting 2 because you have appended the new input outside of your
#testparagraph. Update yoursize()call to check the proper container, by replacing:with