1

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

first i gave 1 then append 1text then i again give 1 then it append two text. 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>');
                }
    });
});

2 Answers 2

1

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank U so much . Very Helpful Answer .
1

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>');
                }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.