javascript - Uncaught TypeError: Cannot read property 'style' of null -


var n1 = document.getelementbyid("n1").style["background-color"]; 

uncaught typeerror: cannot read property 'style' of null

so try change table cell's background color that. not that, says cannot read, can write (i mean can change background color same way can't read value).

code:

function checkdiagonal() {     var n1 = document.getelementbyid("n1").style["background-color"];     var n2 = document.getelementbyid("n2").style["background-color"];     var n3 = document.getelementbyid("n3").style["background-color"];     var n4 = document.getelementbyid("n4").style["background-color"];     var n5 = document.getelementbyid("n5").style["background-color"];     var n6 = document.getelementbyid("n6").style["background-color"];     var n7 = document.getelementbyid("n7").style["background-color"];     var n8 = document.getelementbyid("n8").style["background-color"];     var n9 = document.getelementbyid("n9").style["background-color"];     var n10 = document.getelementbyid("n10").style["background-color"];     if (n1!=='000000' || n2!=='000000' || n3!=='000000' || n4!=='000000' || n5!=='000000' || n6!=='000000' || n7!=='000000' || n8!=='000000' || n9!=='000000' || n10!=='000000')     {         return;     } else {         var winner='nobody';         if (n1 == n2 && n2 == n3) { winner=n3; }             if (n1 == n4 && n4 == n7) { winner=n7; }             if (n1 == n5 && n5 == n9) { winner=n9; }             if (n2 == n5 && n5 == n8) { winner=n8; }             if (n3 == n5 && n5 == n7) { winner=n7; }             if (n3 == n6 && n6 == n9) { winner=n9; }             if (n4 == n5 && n5 == n6) { winner=n6; }             if (n7 == n8 && n8 == n9) { winner=n9; }         if (winner=='nobody') {             document.getelementbyid("n1").style["background-color"]="ffffff";             document.getelementbyid("n4").style["background-color"]="ffffff";             document.getelementbyid("n7").style["background-color"]="ffffff";             document.getelementbyid("n8").style["background-color"]="ffffff";             document.getelementbyid("n9").style["background-color"]="ffffff";         }     } } var st = new playerturnobj(); //pointless explain this, has nothing </script> </head><body> <table align="center" border=0> <tr align="center"><td id="n1" style="background-color:000000" onclick="switchturngen(st); mark('n1'); checkdiagonal('n1');"></td><td id="n2" style="background-color:000000" onclick="switchturngen(st); mark('n2'); checkdiagonal('n2');"></td><td id="n3" style="background-color:000000" onclick="switchturngen(st); mark('n3'); checkdiagonal('n3');"></td></tr> <tr align="center"><td id="n4" style="background-color:000000" onclick="switchturngen(st); mark('n4'); checkdiagonal('n4');"></td><td id="n5" style="background-color:000000" onclick="switchturngen(st); mark('n5'); checkdiagonal('n5');"></td><td id="n6" style="background-color:000000" onclick="switchturngen(st); mark('n6'); checkdiagonal('n6');"></td></tr> <tr align="center"><td id="n7" style="background-color:000000" onclick="switchturngen(st); mark('n7'); checkdiagonal('n7');"></td><td id="n8" style="background-color:000000" onclick="switchturngen(st); mark('n8'); checkdiagonal('n8');"></td><td id="n9" style="background-color:000000" onclick="switchturngen(st); mark('n9'); checkdiagonal('n9');"></td></tr> </table> </body></html> 

edit: found out problem. problem @ id n10, there ids until n9, n10 caused error. sorry asking then

try this:

var n1 = window.getcomputedstyle(document.getelementbyid("n1"),null).backgroundcolor; 

it'll give color in rgb.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -