title: Weird value for counter variable in javascript
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1451009/javascript-infamous-loop-problem">Javascript infamous Loop problem?</a>  </p>
  </blockquote>
  
  
  
  <p>For some reason I get "6" inside my function here for every div element:</p>
  
  <pre><code>for(var i = 1; i &lt;= 5; i++){
    $('&lt;div class="e-' + i + '"&gt;&lt;/div&gt;').appendTo(something).click(function(){
      alert(i);  // &lt;-- it's 6. wtf??
    });
  }
  </code></pre>
  
  <p>instead of 1, 2, 3, etc.</p>
  
  <p>The class on the other hand it appears to be correctly set..</p>
  
  <p>What am I doing wrong?</p>