i’m listening to a click event. When triggered, I need to bind a different event (for other element) but it keeps triggering both on the first event.
My code:
$(".element1").on("click", function(){
console.log("triggered element1");
$(".element2").on("click", function(){
console.log("triggered element2");
})
});
A separate copy of the event handler for
.element2will get bound every time.element1is clicked. If you want to prevent that, remember in a variable whether you have already bound the handler.An alternative solution would be to unbind the first handler when it runs: