Javascript addEventListener inside a loop
<p>I'm trying to add an event listener to some elements which I generate in the loop. I must use <code>div.lastChild</code> - although in this example it's pretty stupid. But that's just demonstration:</p>

<pre><code>&lt;div id="someArea"&gt;
&lt;/div&gt;
&lt;script type="text/javascript"&gt;
    var func = function() {
        alert('Callback works');
    }

    var func1 = function() {
        alert('Test');
    }

    var func2 = function() {
        alert('Test2');
    }

    var div = document.getElementById("someArea");
    var callbacks = [func, func1, func2];
    for(var i = 0; i &lt;= 2; i++) {
        div.innerHTML += '&lt;input type="button" value="' + i + '" /&gt;';
        (function(i) {
            console.log(div.lastChild);
            div.lastChild.addEventListener('click', callbacks[i], false);
        }(i));

    }
&lt;/script&gt;
</code></pre>

<p>The event works only for the last button. What am I missing here?</p>
