What e.preventDefault() method really does in jQuery?


The preventDefault() method stops the default action of a selected element from happening by a user. This method does not accept any parameter and works in two ways:

  • It prevents a link from following the URL so that the browser can't go another page.
  • It prevents a submit button from submitting a form.

Example

<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   <script>
      $(document).ready(function() {
         $("a").click(function(event) {
            event.preventDefault();
         });
      });
   </script>
</head>
<body>
   <a href="https://www.tutorialspoint.com/">Tutorialspoint.com</a>
   <p>Click the link and it wont work.</p>
</body>
</html>

Updated on: 23-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements