What are Event Methods in jQuery?

jQueryWeb DevelopmentFront End Technology

Commonly used event methods include $(document).ready(), click(), dblclick() etc. There is a list of methods which can be called on an Event Object,

The following are some of the methods which can be called on an Event Object,

S.No
Method and Description
1
preventDefault() 
Prevents the browser from executing the default action.
2
isDefaultPrevented() 
Returns whether event.preventDefault() was ever called on this event object.
3
isPropagationStopped() 
Returns whether event.stopPropagation() was ever called on this event object.
4
stopImmediatePropagation() 
Stops the rest of the handlers from being executed.


Let us see an example of stopPropagation() method. The stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent handlers from being notified of the event.

Example

You can try to run the following code to learn how to work with event method stopPropagation() in jQuery −

Live Demo

<html>

   <head>
      <title>jQuery stopPropagation() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
            $("div").click(function(event){
               alert("This is : " + $(this).text());
               // Comment the following to see the difference
               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>
raja
Published on 19-Dec-2017 14:52:15
Advertisements