14

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.

8
  • can you show the problem in context? Commented Feb 12, 2013 at 18:39
  • Assuming this is just example content, but the $(".anotherdiv").hide(); is not the same as $(".another").hide();
    – David
    Commented Feb 12, 2013 at 18:40
  • Post relavent HTML code. From what I see, both should work, unless you drag out of that without mouse up. Basically click = mousedown + mouseup. Commented Feb 12, 2013 at 18:40
  • Did you wrap your JS-Conde into $(function(){ ... });?
    – Chris
    Commented Feb 12, 2013 at 18:42
  • 1
    It's not the nature of jQuery to behave like you mentioned. It's more of a context oriented problem. So, if you can show us your code we would be able to help.
    – dlock
    Commented Feb 12, 2013 at 18:45

2 Answers 2

22

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?

4
  • Even if the element has another click event handler, it should trigger both. So, that's not the problem. Moreover, even if the mousedown executes before click, removing the mousedown event would certainly make the click event fire normally. So, I think this is not the reason.
    – dlock
    Commented Feb 12, 2013 at 18:45
  • What if the other click handler is given first preference and it has a return false statement in it? Updated this in the answer. :) Commented Feb 12, 2013 at 18:45
  • I assume that a click handler can be given a first preference by writing it before the other handlers. And even if you return false in the first click handler, that shouldn't stop the other handlers from firing.
    – dlock
    Commented Feb 12, 2013 at 18:49
  • 1
    @PraveenKumar: I think that reason two sound really great to me, since there are other click events. I'm not able to post the whole code, since this is from work and I got now connection from here to my sandbox. $(".another").hide() ...should actually read $(".anotherdiv").hide(). sorry for confusing
    – xhallix
    Commented Feb 12, 2013 at 18:51
1

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.