title: Why does this html/javascript combo print out the way it does?
body: <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
      &lt;title&gt;Ovi Maps API Example&lt;/title&gt;
  
    &lt;/head&gt;
    &lt;body&gt;
        &lt;button id="butt1" name="butt" type="button"&gt;Click Me!&lt;/button&gt;
        &lt;button id="butt2" name="butt" type="button"&gt;Click Me!&lt;/button&gt;
        &lt;button id="butt3" name="butt" type="button"&gt;Click Me!&lt;/button&gt;
    &lt;/body&gt;
     &lt;script src="jquery.js" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;
     &lt;script src="foo.js" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and...</p>
  
  <pre><code>$(document).ready(function() {
  //alert("ready");
  var myClickFunctions = new Array();
  
  //add event handlers to all three buttons
  for(var i=1;i&lt;=3;i++){
  
    myClickFunctions[i]=function(){
      var index = i;
      alert(index);
    }
  
    var buttonID = "#butt";
    var button  = $(buttonID + i);    
    button.click(myClickFunctions[i]);
  }
  </code></pre>
  
  <p>});</p>
  
  <p>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?</p>