What is an Event Object in jQuery?


The callback function takes a single parameter; when the handler is called the JavaScript event object will be passed through it.

The event object is often unnecessary and the parameter is omitted, as sufficient context is usually available when the handler is bound to know exactly what needs to be done when the handler is triggered, however there are certain attributes which you would need to be accessed.

Let us see an example of isDefaultPrevented() method. The isDefaultPrevented() method checks whether event.preventDefault() was ever called on this event object.

Example

You can try to run the following code to learn how to work with even object in jQuery:

Live Demo

<html>

   <head>
      <title>jQuery isDefaultPrevented() 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){
               
               if ( event.isDefaultPrevented() ){
                  alert( "Default behavior is disabled - 1" );
               }else{
                  alert( "Default behavior is enabled - 1" );
               }
                   
               event.preventDefault();
                   
               if ( event.isDefaultPrevented() ){
                  alert( "Default behavior is disabled - 2" );
               }else{
                  alert( "Default behavior is enabled - 2" );
               }
            });
               
         });
      </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>

Updated on: 11-Dec-2019

219 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements