Jquery event function trigger once despite of multiple time attach

Total Post:110

Points:776
JQuery 
 1940  View(s)
Ratings:
Rate this:
Hi Expert!

How can i do this

html

   <button id="onclickpopupmenu">Test</button>
   <button id="popupmenuok"></button>
javascript:

$("#onclickpopupmenu").bind("click",function(){

  alert("this can execute multiple times")
  $("#popmenuok").bind("click",function(){
    alert("this has to be triggered only once eventhough the parent event trigger multiple times")
  })
})

Please help me...

Thanks in advance! 

  1. Post:604

    Points:4228
    Re: Jquery event function trigger once despite of multiple time attach

    Hi Jeet!

    Only bind the handler once:

    var popupmenuBound = false;
    $("#onclickpopupmenu").bind("click",function(){
      alert("this can execute multiple times");
      if (!popupmenuBound) {
        $("#popupmenuok").bind("click",function(){
          alert("this has to be triggered only once eventhough the parent event trigger multiple times");
        });
        popupmenuBound = true;
      }
    });

      Modified On Mar-30-2018 12:27:49 AM

Answer