JS Coding Questions Logo
JS Coding Questions
#103💼 Interview💻 Code

What is the use of preventDefault method

Advertisement

728x90

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
1document
2  .getElementById("link")
3  .addEventListener("click", function (event) {
4  event.preventDefault();
5  });

Note: Remember that not all events are cancelable.

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 18

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
103of476