title: JavaScript click handler not working as expected inside a for loop
body: <p>I'm trying to learn JS and got an issue.</p>
  <p>I tried many things and googled but all in vain. Following piece of code doesn't work as expected. I should get value of <code>i</code> on click but it always returns 6. I'm pulling my hair out; please help.</p>
  <pre><code>for (var i = 1; i &lt; 6; i++) {
  
      console.log(i);
  
      $(&quot;#div&quot; + i).click(
          function() {
              alert(i);
          }
      );
  }
  </code></pre>
  <p><a href="http://jsfiddle.net/rmXcF/" rel="noreferrer"><strong>jsfiddle</strong></a></p>