Consider the following code:
$(document).click(function (event) {
console.log("Ok");
});
$(document).add($('p')).click(function onceHandler(event) {
console.log('Clicked.');
});
I would like to make the onceHandler
run only once when "p" is clicked, so each handler should run once instead of the second one running twice due to the click propagating from p to document.
event.stopPropagation()
will break the first handler, so I can't use it. I've also tried:
$(document).add($('p')).click(function(event) {
if (event.stopDoingThat) return;
console.log('Clicked.');
event.stopDoingThat = true;
});
which didn't work. So basically without changing anything I'm getting 2 "clicked" and one "ok". With stopPropagation
- 1 "clicked", what I need is 1 "clicked" and 1 "ok"