I have a label and some functions running while clicking on it.
But when a click event is made, a double click event is done, then my functions run 2 times...
You can see a light example here
HTML:
<label>
<input type="checkbox" id="checkbox"> Click here
</label>
<input type="text" id="test" value="0"/> clicks
JavaScript:
$(document).ready(function(){
$('label').click(function(event) {
$('#test').val(parseInt($('#test').val())+1);
event.preventdefault();
});
});
How to solve this problem ?
Edit
preventdefault()
to preventDefault()
fixed the double click, but now checkbox is not checked anymore...
on('click',function(e){ if(e. originalEvent. detail > 1){ return; /* if you are returning a value from this function then return false or cancel the event some other way */ } }); Done.
To prevent multiple button clicks in React: Set an onClick prop on the button, passing it a function. When the button gets clicked, set its disabled attribute to true .
Typically, a single click initiates a user interface action and a double-click extends the action. For example, one click usually selects an item, and a double-click edits the selected item.
Well this is interesting. You're seeing two click
events, one of them from the checkbox input
, and the other (of course) from the label
. And it makes sense: Clicking a label
is like clicking the checkbox the label
labels, by design. Here's an updated fiddle showing what's happening.
So just hook click
on the checkbox and don't hook it on the label:
$(document).ready(function(){
$('#checkbox').click(function(event) {
$('#test').val(parseInt($('#test').val())+1);
});
});
Updated fiddle
And as Rocket said in the question's comments: preventDefault
has a capital D
in it, so your code was throwing an exception. But you didn't want preventDefault
anyway, because you want the checkbox to be checked.
A simple workaround is to ignore one of the call.. See below,
$(document).ready(function(){
$('label').click(function(event) {
if (event.target.nodeName == 'LABEL') return;
$('#test').val(parseInt($('#test').val())+1);
});
});
DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With