

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
What is the difference between event.preventDefault() and event.stopPropagation() in jQuery?
stopPropogation() method
To stop the bubbling of an event to parent elements, use the stopPropagation() method.
Example
You can try to run the following code to learn how to work with stopPropogation() method:
<html> <head> <title>jQuery stopPropagation() method</title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript" language = "javascript"> $(document).ready(function() { $("div").click(function(event){ alert("This is : " + $(this).text()); event.stopPropagation(); }); }); </script> <style> div { margin:10px; padding:12px; border:2px solid #666; width:160px; } </style> </head> <body> <p>Click on any box to see the effect:</p> <div id = "div1" style = "background-color:blue;"> OUTER BOX <div id = "div2" style = "background-color:red;"> INNER BOX </div> </div> </body> </html>
preventDefault() method
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.qries.com">Qries</a> </body> </html>
- Related Questions & Answers
- What is the difference between event.preventDefault() and return false in jQuery?
- What is the difference between jQuery and JavaScript?
- What is the difference between jQuery and AngularJS?
- What is the difference between jQuery(selector) and $(selector)?
- What is the difference between Ajax and jQuery-Ajax methods in jQuery?
- What is the difference between filter() and find() in jQuery?
- What is the difference between text() and html() in jQuery?
- What is the difference between Grep and Filter in jQuery?
- What is the difference between jQuery.children( ) and jQuery.siblings( ) in jQuery?
- What is the difference between append() and appendTo() in jQuery?
- What is the difference between height and innerHeight in jQuery?
- What is the difference between height and outerHeight in jQuery?
- What is the difference between innerHeight and outerHeight in jQuery?
- What is the difference between width and innerWidth in jQuery?
- What is the difference between width and outerWidth in jQuery?
Advertisements