Black friday

Save big!

All courses under $5 - for a limited time!

Code has been added to clipboard!

Stop Event Default Actions With jQuery preventDefault Method

Reading time 1 min
Published Dec 29, 2017
Updated Sep 27, 2019

jQuery preventDefault: Main Tips

  • The event.preventDefault method prevents the event's default action from being executed.
  • The event.isDefaultPrevented is used to check whether the event handler triggered the jQuery preventDefault method.

event.preventDefault Explained

The jQuery event.preventDefault method disturbs the actions of events and prevents them from being completed. For instance, the example below shows that the event preventDefault will not allow an URL to be opened after it is clicked:

Example
$("a").click((event) => {
   event.preventDefault();
});

Invoking the jQuery preventDefault will cancel the event. Therefore, the default action of the event will not take place, just like the URL is not opened in the code example. Remember that the method does not take any arguments: it is simply written as event.preventDefault().