I'm trying to wrap the logic inside the function so that I can apply the same function across the whole project.
This works below:
$(selector).on('click', function(){
var hasBeenCliked = $(this).attr("has-been-clicked");
if (hasBeenCliked === "yes") {
return;
}
$(this).attr("has-been-clicked", "yes");
//do some stuff once
});
Now, when I create a function something like this:
function preventTwiceClick(item){
var hasBeenCliked = $(this).attr("has-been-clicked");
if (hasBeenCliked === "yes") {
return;
}
$(this).attr("has-been-clicked", "yes");
}
and then reuse it in multiple times on any click function like this, it doesn't work and it is still firing twice:
$(selector1).on('click', function(){
preventTwiceClick(this);
//do some stuff
});
$(selector2).on('click', function(){
preventTwiceClick(this);
//do some stuff
});
$(selector3).on('click', function(){
preventTwiceClick(this);
//do some stuff
});
Many thanks for your help
EDIT I did what you suggested guys, but it is not working. Here's the example: FIDDLE
.one()instead of.on()?