today I discovered something that was quite confusing for me. I just tried to hide s.th via jquery... first I tried to use this
$(".specificdiv li:nth-child(3)").click(function(){
$(".anotherdiv").hide();
})
....but it does not work.
After a time I tried it this way:
$(".specificdiv li:nth-child(3)").mousedown(function(){
$(".anotherdiv").hide();
})
Can anyone explain me why mousedown works instead of click? Would be great to find out
EDIT
edited the anotherdiv.
Note: This differs from the click event in that click is fired after a full click action occurs; that is, the mouse button is pressed and released while the pointer remains inside the same element. mousedown is fired the moment the button is initially pressed.
MouseDown occurs when the user presses the mouse button; MouseUp occurs when the user releases the mouse button.
Mouseup is always firing before click, despite this order.
jQuery mousedown() Method The mousedown event occurs when the left mouse button is pressed down over the selected element. The mousedown() method triggers the mousedown event, or attaches a function to run when a mousedown event occurs. Tip: This method is often used together with the mouseup() method.
Possible reasons:
mousedown
executes before click
, so first come first serve.click
event, which prevents this from happening, say that function executes first and it has a return false
statement in it.Now since you are using mousedown
, which is not assigned for this element, it doesn't have any conflicts. This may be a reason, because you didn't post the full code. Feel free to correct. :)
On a smaller note, you have $(".anotherdiv").hide();
in the first code and $(".another").hide();
in the second code, missing the div
in the class. Is that a problem?
Actually the mousedown event is triggered when you push down the button, even you don't let the button up. The click is like the mouseup, when you let the button go up.
In your code you have: ".anotherdiv" and ".another" could it be your error?
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