We have a function:
function createMatrix() { var matrix = document.getElementById('matrix'); var n = 20 * 20; for (var i = 0; i < n; i++) { var div = document.createElement('div'); div.setAttribute('id', 'div_' + i); div.className = 'cell'; matrix.appendChild(div); } r = (randomcell(0,399)); small_div = document.getElementById('div_' + r); small_div.className = 'black'; }
Outside of this function, I write for example: small_div.setAttribute('id', 'div_13');
Just to check if it works. Writes "small_div is not defined". Why? It seems that without var the declaration goes to the function? I am new to javascript. Help, please, with features when working with objects in Javascript. Thank you in advance.