jQuery event.data Property


The event.data property in jQuery contains the optional data passed to an event method when the current executing handler is bound.

Syntax

The syntax is as follows −

event.data

Example

Let us now see an example to implement the jQuery event.data property −

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("p").each(function(i){
         $(this).on("click", {a:i}, function(event){
            alert("Line " + event.data.a);
         });
      });
   });
</script>
</head>
<body>
<h2>Demo Heading</h2>
<p>This is the 1st paragraph.</p>
<p>This is the 2nd paragraph.</p>
<span>Click on any of the p element above</span>
</body>
</html>

Output

This will produce the following output −

Click on any of the p element −


Updated on: 11-Nov-2019

154 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements