jQuery event.PreventDefault

The preventDefault() method tells the browser that if there is a default behaviour for this event on this object, then skip that default behaviour. This is not a jQuery feature , but it is part of the Event object passed to Javascript event listeners . When you bind an event listener to a DOM (Document Object Model) element the Event object is passed as an argument. That listener is executed when that event is triggered, but each event has a default implementation . When you call event.preventDefault is flags the event object as handled, and the default implementation is skipped.

For example, if you set up a click handler for a link and you call e.preventDefault() in that click handler, then the browser will not process the click on the link and will not follow the href in the link.

It's most noticeable with < a > tags when the click event, because you might want to prevent the link from being followed. But, it also works on things like keyboard input , touch, and so on.

If the hyperlink above is clicked then the browser will not try to redirect the browser (because of the preventDefault() call), but rather it will instead do the rest of the processing and append data to the div instead.

run this source code Try it Yourself
Go to website

Full Source

event.preventDefault() Vs. event.stopPropagation()

  1. event.preventDefault() – It stops the browsers default behaviour.

  2. event.stopPropagation() – It prevents the event from propagating (or bubbling up) the DOM.

 (C) 2023    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About