-4

I have an input (type: button), and I want to call for that an onclick event. But sometimes I added the input button later, and then I have to call again the event. So I have an input button with 2 onclick event. How can I check that onclick event already called for that?

Thanks

mikecpl
  • 68
  • 8

2 Answers2

1
var counter=0;
$("#btn").onClick(function(){
    counter+=1;
    // do some
});

then u can check the value of counter:

if (counter>0){
    alert("clicked before");
}
behnamizadi
  • 133
  • 2
  • 8
1

Instead of binding event handlers again for dynamically generated elements, you can delegate the event handler to a static (preferable the closest parent element which is not dynamically generated) parent element using the on() method.

For example:

$(document).on("click",":button",function(){});

Will work with the buttons you generate dynamically in future as well. So no need to bind events again which is causing the issue for you.

T J
  • 42,762
  • 13
  • 83
  • 138