event.data Property - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:event.data

jQuery event.data Property

Description

The event.data property contains the optional data passed to an event method.

Syntax

Parameter Description
event Required. The event parameter comes from the event binding function

The following code shows how to Return the data passed with the on() method for each <p> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("p").each(function(i){
        $(this).on("click", {x:i}, function(event){
            console.log("The " + $(this).index() + ". paragraph has data: " + event.data.x);
        });/*  w w  w  . j  a  va 2s.c  o m*/
    });
});
</script>
</head>
<body>

<p>Click on each p element to return the data passed with the on() method.</p>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>

Related Tutorials