Cannot read property "style" of null help. [Javascript, HTML, CSS, JQuery] -


this project works fine on jsfiddle when compile file doesn't work. every thing involves changing "style" gives error in source/console.

this simple error can't figure out.

link fiddle: http://jsfiddle.net/enmydky4/

  • code -

html:

<!doctype html> <head>     <title> color test </title>     <link rel="stylesheet" type="text/css" href="css.css">     <script src = "jquery-2.1.3.js"></script>     <script src="javascript.js"></script>     <meta charset="utf-8"> </head> <html> <body>     <table id="table">         <tr id="row1">             <td></td>             <td></td>             <td></td>             <td></td>             <td></td>         </tr>         <tr id="row2">             <td></td>             <td></td>             <td></td>             <td></td>             <td></td>         </tr>         <tr id="row3">             <td></td>             <td></td>             <td></td>             <td></td>             <td></td>         </tr>         <tr id="row4">             <td></td>             <td></td>             <td></td>             <td></td>             <td></td>         </tr>         <tr id="row5">             <td></td>             <td></td>             <td></td>             <td></td>             <td></td>         </tr>     </table>     <div id="start">start</div>     <div id="time"> time: </div> </body> </html> 

javascript:

var items = document.queryselectorall("td"); var itemnum = 0; var correct = 0; var time = 15; var random = function (multiplyer) {     return string(math.round(math.random() * multiplyer)); }; var funcrgb = function () {     var rgb = [random(255), random(255), random(255)].join(",");     return rgb; }; var cleardata = function(){     itemnum = 0;     correct = 0;     time = 15; };  var starttimer = function(){     var timer = setinterval(function(){          if (time > 0){             time = time - 1;             document.getelementbyid("time").innerhtml = "time: " + time;         }else{             alert("finished" + "\n" + "correct: " + string(correct) + "\n" +  "total: " + string(itemnum) + "\n" + "percentage: "  + (correct/itemnum)  * 100 + "%" );             clearinterval(timer);             cleardata();             startgame();         }     }, 1000);  }; var startgame = function () {     var startscreen = document.getelementbyid("start");     startscreen.style.display = "none";     starttimer();     setsquares(); }; var setsquares = function () {     var rgb = funcrgb();     var different = math.round(math.random() * 24);     (var = 0; <= items.length; i++) {         if (i == different) {             items[i].style.backgroundcolor = "rgba(" + rgb + ",.8" + ")";             items[i].id = itemnum;         } else {             items[i].style.backgroundcolor = "rgb(" + rgb + ")";         }     } }; window.onload = function(){     document.getelementbyid("start").onclick = startgame; }  $("#start").click(function() {     $(this).fadeout(); }); $( "td" ).click(function() {     $(this).toggle("bounce");     if ($(this).attr("id") == itemnum) {         correct = correct + 1;     }     itemnum = itemnum + 1;     setsquares(); });        var starttimer = function(){         var timer = setinterval(function(){              if (time > 0){                 time = time - 1;                 document.getelementbyid("time").innerhtml = "time: " + time;             }else{                 //alert("finished" + "\n" + "correct: " + string(correct) + "\n" +  "total: " + string(itemnum) + "\n" + "percentage: "  + (correct/itemnum)  * 100 + "%" );                 clearinterval(timer);                 cleardata();                 startgame();             }         }, 1000);      };     var startgame = function () {         var startscreen = document.getelementbyid("start");         //$(startscreen).toggle( "bounce", { times: 3 }, "slow" );         //startscreen.style.display = "none";         starttimer();         setsquares();     };     var setsquares = function () {         var rgb = funcrgb();         var different = math.round(math.random() * 24);         (var = 0; <= items.length; i++) {             if (i == different) {                 items[i].style.backgroundcolor = "rgba(" + rgb + ",.8" + ")";                 items[i].id = itemnum;             } else {                 items[i].style.backgroundcolor = "rgb(" + rgb + ")";             }         }     };      document.getelementbyid("start").onclick = startgame;     $("#start").click(function() {         $(this).fadeout();     });     $( "td" ).click(function() {         if ($(this).attr("id") == itemnum) {             correct = correct + 1;         }         itemnum = itemnum + 1;         setsquares();     }); 

wrap code in document.ready handler. short way in jquery put above code in between $(function(){...}) like

$(function(){ var items = document.queryselectorall("td"); var itemnum = 0; var correct = 0; var time = 15; var random = function (multiplyer) {     return string(math.round(math.random() * multiplyer)); }; var funcrgb = function () {     var rgb = [random(255), random(255), random(255)].join(",");     return rgb; }; var cleardata = function(){     itemnum = 0;     correct = 0;     time = 15; };  var starttimer = function(){     var timer = setinterval(function(){          if (time > 0){             time = time - 1;             document.getelementbyid("time").innerhtml = "time: " + time;         }else{             alert("finished" + "\n" + "correct: " + string(correct) + "\n" +  "total: " + string(itemnum) + "\n" + "percentage: "  + (correct/itemnum)  * 100 + "%" );             clearinterval(timer);             cleardata();             startgame();         }     }, 1000);  }; var startgame = function () {     var startscreen = document.getelementbyid("start");     startscreen.style.display = "none";     starttimer();     setsquares(); }; var setsquares = function () {     var rgb = funcrgb();     var different = math.round(math.random() * 24);     (var = 0; <= items.length; i++) {         if (i == different) {             items[i].style.backgroundcolor = "rgba(" + rgb + ",.8" + ")";             items[i].id = itemnum;         } else {             items[i].style.backgroundcolor = "rgb(" + rgb + ")";         }     } }; window.onload = function(){     document.getelementbyid("start").onclick = startgame; }  $("#start").click(function() {     $(this).fadeout(); }); $( "td" ).click(function() {     $(this).toggle("bounce");     if ($(this).attr("id") == itemnum) {         correct = correct + 1;     }     itemnum = itemnum + 1;     setsquares(); });        var starttimer = function(){         var timer = setinterval(function(){              if (time > 0){                 time = time - 1;                 document.getelementbyid("time").innerhtml = "time: " + time;             }else{                 //alert("finished" + "\n" + "correct: " + string(correct) + "\n" +  "total: " + string(itemnum) + "\n" + "percentage: "  + (correct/itemnum)  * 100 + "%" );                 clearinterval(timer);                 cleardata();                 startgame();             }         }, 1000);      };     var startgame = function () {         var startscreen = document.getelementbyid("start");         //$(startscreen).toggle( "bounce", { times: 3 }, "slow" );         //startscreen.style.display = "none";         starttimer();         setsquares();     };     var setsquares = function () {         var rgb = funcrgb();         var different = math.round(math.random() * 24);         (var = 0; <= items.length; i++) {             if (i == different) {                 items[i].style.backgroundcolor = "rgba(" + rgb + ",.8" + ")";                 items[i].id = itemnum;             } else {                 items[i].style.backgroundcolor = "rgb(" + rgb + ")";             }         }     };      document.getelementbyid("start").onclick = startgame;     $("#start").click(function() {         $(this).fadeout();     });     $( "td" ).click(function() {         if ($(this).attr("id") == itemnum) {             correct = correct + 1;         }         itemnum = itemnum + 1;         setsquares();     });  }); 

the reason working in jsfiddle not actual site because fiddle loading script in onload event.

in actual site, including script in head, following line fail

var startscreen = document.getelementbyid("start"); 

since there not yet element id 'start'


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -