<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Ovi Maps API Example</title>
</head>
<body>
<button id="butt1" name="butt" type="button">Click Me!</button>
<button id="butt2" name="butt" type="button">Click Me!</button>
<button id="butt3" name="butt" type="button">Click Me!</button>
</body>
<script src="jquery.js" type="text/javascript" charset="utf-8"></script>
<script src="foo.js" type="text/javascript" charset="utf-8"></script>
</html>
and...
$(document).ready(function() {
//alert("ready");
var myClickFunctions = new Array();
//add event handlers to all three buttons
for(var i=1;i<=3;i++){
myClickFunctions[i]=function(){
var index = i;
alert(index);
}
var buttonID = "#butt";
var button = $(buttonID + i);
button.click(myClickFunctions[i]);
}
});
Every button prints 4. Why is this and what is a good way to make each one print the value of i
in which the handler was created?