jQuery API

event.data

event.data

Description: An optional data map passed to an event method when the current executing handler is bound.

  • version added: 1.1event.data

Example:

Within a for loop, pass the value of i to the .on() method so that the current iteration's value is preserved.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
<button> 0 </button>
<button> 1 </button>
<button> 2 </button>
<button> 3 </button>
<button> 4 </button>

<div id="log"></div>

<script>
var logDiv = $("#log");

/* Note: This code is for demonstration purposes only. */
for (var i = 0; i < 5; i++) {
  $("button").eq(i).on("click", {value: i}, function(event) {
    var msgs = [
      "button = " + $(this).index(),
      "event.data.value = " + event.data.value,
      "i = " + i
    ];
    logDiv.append( msgs.join(", ") + "<br>" );
  });
}
</script>

</body>
</html>

Demo:

Support and Contributions

Need help with event.data or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to event.data? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • Dave Wood

    Worth mentioning that, as I understand it, .data is simply an associative array (like a hash map).

    It is not the jQuery.data() function that is attached to JQuery objects: http://api.jquery.com/jQuery.data/

    So you can say:

    var myData = event.data.someData;

    Or even:

    var myData = event.data['someData'];

    But definitely not:

    var myData = event.data('someData');

  • Wyllyamoliveira

    sdfsda

  • Wyllyamoliveira

    we