Cannot read property 'addEventListener' of undefined <pre><code> function asign(id){ return document.getElementById(id) ; } var b = ['p','q','r','s','t','u','v']; var a = ['fname','lname','email','password','r_password','g_m',"g_f"] ; for (i=0;i&lt;a.length;i++) { var x = {} ; x[b[i]] = asign(a[i]) ; x[i].addEventListener('click', function() { alert(x[i].value) ;} ,false) ; } </code></pre> <p>I want just array of variables and IDs asign with them in for loop .</p>