An optional object of data passed to an event method when the current executing handler is bound.

.event.data()🡢 Object

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


<div id="log"></div>
var logDiv = $("#log");

for (var i = 0; i < 5; 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>");

Looking for a Web Developer?


Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)