50
rated 0 times
[
52]
[
2]
/ answers: 1 / hits: 47814
/ 13 Years ago, wed, january 18, 2012, 12:00:00
Refactoring standard onClick
within html tag to listeners ,faced problem with my code:
var td;
for (var t=1;t<8;t++){
td = document.getElementById('td'+t);
if (typeof window.addEventListener==='function'){
td.addEventListener('click',function(){
console.log(td);
})}
}
When td
element is clicked on,it's assumed that clicked td
with last index from loop,e.g. 7
Looks like ,eventListeners
been populated for last element in this loop only.
Loop initialization looks correct.
Why so happened?
Here is live code
More From » events