Q&A
Ask and answer questions to make information more available to wider audiences.
Gabriela Michael @michaelgabriela5   04, Jul 2023 12:00 AM
event.preventDefault() method
What is the use of event.preventDefault() method?
answers 2
 
Answer 1
Nathanael Chandler @chandlernathanael   11, Jul 2023 02:52 PM
Example - Assume you're developing an AJAX-based application in which you need to submit form data through AJAX when a button in the form is clicked. So, if you click on a button without preventing it, the button behaves normally and the website reloads. As a result, the other event will not occur when you click the button. So, if we use prevent default there, we can skip the button's default behaviour and instead perform the AJAX event and alter the HTML DOM accordingly.

 
Answer 2
Nathanael Chandler @chandlernathanael   11, Jul 2023 02:52 PM
Event.PreventDefault is a jQuery event that stops an event from firing when the user clicks on a link or other trigger. It basically stops the default behaviour of the triggers and allows any other event to occur. It is useful for preventing unwanted events from firing when a user clicks on a link or other trigger. This can be useful when you want to prevent users from accidentally clicking on links that open up new windows or tabs.

When you use an event like click or keypress, you can prevent the event from firing by adding the event.preventDefault() method to the event object. When you use event.preventDefault(), the event will not fire unless the user clicks on a link or other trigger.

Event.preventDefault() is only available in jQuery 1.7 and later versions of jQuery.