title: Setting onclick to use current value of variable in loop
body: <p>The title may not make sense and nor would a description alone, so here's a sample of the code:</p>
  
  <pre><code>for(var a=0;a&lt;10;a++) {
      var b=document.createElement('b')
      b.onclick=function() {
          alert(a)
      }
      b.innerHTML=a
      document.body.appendChild(b)
  }
  </code></pre>
  
  <p>What's odd is that when I set innerHTML to a, it uses the current value of a. So, this code creates ten <code>&lt;b&gt;</code> elements with the values 0, 1, 2, 3, 4, 5, 6, 7, 8, &amp; 9. This works perfectly. What doesn't, however, is the onclick function. It returns the final value of <code>a</code> (10) when any of these elements is clicked. I've tried setting another variable to <code>a</code> and then using that other variable in the onclick event, but still it returns the final value of <code>a</code>. How would I make it use the value of <code>a</code> when onclick is set?</p>