Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery mousedown vs click

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.

like image 979
xhallix Avatar asked Feb 12 '13 18:02

xhallix


People also ask

Is Mousedown the same as click?

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.

What is the difference between mouseup and Mousedown?

MouseDown occurs when the user presses the mouse button; MouseUp occurs when the user releases the mouse button.

Does mouseup fire before click?

Mouseup is always firing before click, despite this order.

What is the function of the Mousedown () method?

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.


2 Answers

Possible reasons:

  1. The event mousedown executes before click, so first come first serve.
  2. The element might have already a 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?

like image 197
Praveen Kumar Purushothaman Avatar answered Oct 03 '22 17:10

Praveen Kumar Purushothaman


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?

like image 36
walter Avatar answered Oct 03 '22 18:10

walter