title: How to do early binding for event handler in JavaScript? (example with jQuery)
body: <p>JavaScript's late binding is great. But how do I early bind when I want to?</p>
  
  <p>I am using jQuery to add links with event handlers in a loop to a div. The variable 'aTag ' changes in the loop. When I click the links later, all links alert the same message, which is the last value of 'aTag'. How do I bind a different alert message to all links?</p>
  
  <p>All links should alert with the value that 'aTag' had when the event handler was added, not when it was clicked.</p>
  
  <pre><code>for (aTag in tagList) {
    if (tagList.hasOwnProperty(aTag)) {
      nextTag = $('&lt;a href="#"&gt;&lt;/a&gt;');
      nextTag.text(aTag);
      nextTag.click(function() { alert(aTag); });
      $('#mydiv').append(nextTag);
      $('#mydiv').append(' ');
    }
  }
  </code></pre>