I have a question about looping in JavaScript. Mostly I use jQuery but now I've decided to make an easy game in pure JavaScript.
if I loop through all my 'td' with this method it works, The cells[i] are td elements and I can attach events to them.
for(i = 0; i < cells.length; i++){
if(cells[i].nodeName == 'TD')
{
cells[i].onclick = function(){
// call funciton on onclick
};
}
}
But if I do like this, each element are just index numbers and the two at the end are length and item.
for(var cell in cells){
// cell is a number
}
What's the difference and why doesn't the foreach-loop work like I want it to?