
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between event.preventDefault() and return false in jQuery
Returning false from jQuery event handlers is like calling both preventDefault() and stopPropagation(). The preventDefault() method prevents the browser from executing the default action.
Example
You can try to run the following code to run event.preventDefault() method in jQuery −
<html> <head> <title>jQuery preventDefault() method</title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script> $(document).ready(function() { $("a").click(function(event){ event.preventDefault(); alert( "Default behavior is disabled!" ); }); }); </script> </head> <body> <span>Click the following link and it won't work:</span> <a href = "https://www.google.com">GOOGLE Inc.</a> </body> </html>
Example
You can try to run the following code to return false from jQuery event handlers −
<html> <head> <title>jQuery return false</title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script> $(document).ready(function() { $("a").click(function(event){ alert( "Default behavior is disabled!" ); return false; }); }); </script> </head> <body> <span>Click the following link and it won't work:</span> <a href = "https://www.google.com">GOOGLE Inc.</a> </body> </html>
Advertisements