<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title>Click on each header element to return the data passed with the on() method</title>
</head>
<body>
<p>Click on each header element to return the data passed with the on() method.</p>
<h3>This is a header1.</h3>
<h3>This is another header2.</h3>
</body>
</html>
JavaScript Code:
$("h3").each(function(i){
$(this).on("click", {x:i}, function(event){
var info = "Header" + $(this).index() + ". paragraph has Event data: " + event.data.x;
$("<p>"+info+"</p>").appendTo( "body" );
});
});
HTML Code:
JavaScript Code: