Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using event.preventDefault() in "mousedown" prevent "click" or "mouseup" event in jquery?

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

like image 832
Madhu Avatar asked Feb 20 '14 06:02

Madhu


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 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.

What is the purpose of event preventDefault () in Javascript event handling?

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form. Clicking on a link, prevent the link from following the URL.

What is Mousedown and mouseup?

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


Video Answer


2 Answers

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

like image 118
Felix Avatar answered Oct 31 '22 18:10

Felix


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.

like image 39
Quinn Avatar answered Oct 31 '22 18:10

Quinn