Reputation: 86
When i give input (1) then its successfully append text. But when i erase then input value and again give input(2) then its increasing with previous value(1+2=3). Where loop count previous and present value
$(document).ready(function(){
var i=0;
$("#flatcount").blur(function() {
var floor = (this).value;
for(i=1;i<=floor;i++) {
$('#row').append('<p>This is appended text</p><br>');
}
});
});
Upvotes: 1
Views: 509
Reputation: 22500
You need to empty the element.before append like $('#row').empty()
$(document).ready(function() {
var i = 0;
$("#flatcount").blur(function() {
var floor = $(this).val();
$('#row').empty() //empty
for (i = 1; i <= floor; i++) {
$('#row').append('<p>This is appended text</p><br>');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="flatcount">
<p id="row"></p>
Upvotes: 1
Reputation: 81
use this code i think it will work
$(document).ready(function(){
var i=0;
$("#flatcount").blur(function() {
$("#row").empty();
var floor = (this).value;
for(i=1;i<=floor;i++) {
$('#row').append('<p>This is appended text</p><br>');
}
});
});
Upvotes: 1