18

I am new to jquery and i have a doubt whether using events.preventDefault() in the mousedown or mouseup events does prevent the click or dblclick event?

Please provide me a clarification or a sample.

Thanks in advance. Madhu

Madhu
  • 2,416
  • 3
  • 15
  • 33
  • events.preventDefault() only prevent the default action of event. – Deepu Sasidharan Feb 20 '14 at 07:01
  • After the reading the other Felix' answer, I think I understand your question now. You should adjust the title though, because it is misleading. – Felix Kling Feb 20 '14 at 07:08
  • Thank you I got what i want. StackOverFlow Rockzzzzzzzzzzzz.... – Madhu Feb 20 '14 at 10:38
  • @Madhu, could you please give the example of how did you get this. – Sonu K Jan 15 '16 at 10:36
  • @SonuK, the comment stackoverflow.com/questions/21900470#comment-33167721 clarified me and in my case a new element is inserted when `mousedown` and hence the target changed and subsequent `click` get prevented. I do some design changes to resolve this. – Madhu Jan 18 '16 at 03:40
  • It does prevent onchange from fireing if you use it in onmousedown – John Jun 22 '17 at 01:23

2 Answers2

12

Neither of mouseup or mousedown prevent the default click event.

Fiddle Demo

You need to use click():

$('#test').on('click', function(e) {
    e.preventDefault();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div onclick="alert('Clicked')" id="test">Click Here</div>

Fiddle Demo

Justin Liu
  • 581
  • 6
  • 21
Felix
  • 37,892
  • 8
  • 43
  • 55
  • 2
    I know that buddy, but i want to know whether it will prevent the sub-sequent events from happening? – Madhu Feb 20 '14 at 07:24
  • 1
    No, it's not. e.preventDefault() just prevent the default action of your element not subsequent elements or other events – Felix Feb 20 '14 at 07:26
3

It does not prevent the event itself, but the action that is triggered by the event.

A simple example would be clicking on an anchor link. The default action of the click event is to take the browser to a new URL. In this case, it won't happen.

Quinn
  • 458
  • 2
  • 8
  • Thanks.I thought it prevents the sub-sequent events from happening. Does it prevent them? – Madhu Feb 20 '14 at 07:23
  • 6
    It does not. If you want to prevent subsequent events you'd need to use either `event.stopPropagation()` or `event.stopImmediatePropagation()` – Quinn Feb 20 '14 at 07:51
  • 4
    @Quinn Doesn't prevent further events for me. I wouldn't have thought so either, since what should propagation have to do with it? – John Oct 10 '18 at 15:34