JQuery: add once and stop (worked in older versions)

So here is the code I'm using:

$(function(){
    $('<div id="info" style="width:auto; height:auto;"/>').load('test.php?var='+d+' #se', function() {
        $('#main').append(this);
    });
});
}

Now, when I run the function (with the keyboard in the input field), the loaded div is added again and again to each keyup event.

I use jQuery 1.5 - in older versions, namely 1.2.6, the div was not added after the first time, and it worked fine (error?).

How to prevent div from being added over and over? (disable the "trigger", unfortunately, is not an option). I also tried appendTo, which is pretty much the same problem.

Someone will help me! Many thanks

+3
source share
2 answers

It looks like you can use .onea keyboard handler to bind:

$('whatever').one('keyup', function() {
    $('<div id="info" style="width:auto; height:auto;"/>').load('test.php?var='+d+' #se', function() {
        $('#main').append(this);
    });
});

.one , .bind, , .

+2

$(function(){
   if($("#info").length==0)
   {
      $('<div id="info" style="width:auto; height:auto;"/>').load('test.php?var='+d+' #se', function() {
        $('#main').append(this);
      });
   }
});
}

, "#info" .

0

Source: https://habr.com/ru/post/1790840/


All Articles