InterviewHubPRO
Section Overview
Q107

Question 107 of 521

Part 3: DOM, Event Flow, Timers & Browser APIs (104 - 173)

21%
JavaScript Internals (521 Qs)•Part 3: DOM, Event Flow, Timers & Browser APIs (104 - 173)

What is the use of preventDefault method

The preventDefault() method cancels the event if it is cancelable, meaning that the default action or behaviour that belongs to the event will not occur. For example, prevent form submission when clicking on submit button and prevent opening the page URL when clicking on hyperlink are some common use cases.

javascript
  document
    .getElementById("link")
    .addEventListener("click", function (event) {
      event.preventDefault();
    });

Note: Remember that not all events are cancelable.