I have the following script:
$(document).ready(function() {
var i = 0;
$("#add_row").click(function() {
if(i <= 5)
{
$('#addr' + i).html("<div><input type='text' name='cirurgia' value="+i+"></div>");
$('#input').append('<div id="addr' + (i + 1) + '">');
i++;
}
});
$("#delete_row").click(function()
{
if (i > 1)
{
$("#addr" + (i - 1)).html('');
i--;
}
});
});
When the user clicks the <div id="add_row">
, the script adds an input and if the user clicks the <div id="delete_row">
, deletes the last added input.
So far the script is working correctly, however it happens that to add the 1st input
the user needs to click twice on the "add_row" button. Then to add the 2nd, 3rd, 4th and 5th% with% is just click once even normally. Why does this happen?
Simple HTML:
<input type="button" id="delete_row" value="deletar">
<input type="button" id="add_row" value="adicionar">
<br>
<div id="input">
</div>