For example, there is a handler

$("#btn1").click(function(){ $('body').append("<div class='test'></div>"); }) 

How to allow execution only 1 time?

  • $ ('# btn1'). one ('click', function () {}); - soledar10

1 answer 1

Use the .one() method to do this:

 $("#btn1").one('click', function(){ $('body').append("<div class='test'>Something</div>"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='button' id='btn1' value='Click me!' /> 

  • and if without jQuery? - Demon __ ANT
  • And what without the jigger? We hang not the user-defined function itself, but the wrapper, which, after triggering, deletes itself with the removeEventListener method and starts the function itself. - user207618
  • I understand the questions there is no thank you :) - Demon __ ANT