0 votes
in JavaScript by
What is the use of preventDefault method in Javascript?

1 Answer

0 votes
by

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.

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

Note: Remember that not all events are cancelable.

...