I have been working on a simple game menu for a game (That I have yet to do.) The code works perfectly in jsfiddle however when I open my local html document none of the javascript works, (console.log and other code works before my error) and I get an error, on my createMenu function: line 67, that says Uncaught TypeError: Cannot read property 'style' of undefined
Here's the code for the createMenu function (also on jsfiddle)
var menu = document.getElementsByClassName("menu");
var menu2 = document.getElementsByClassName("menu2");
var createMenu = function()
{
menu[0].style.display = "block"; <---- Error
menu[1].style.display = "block";
menu[2].style.display = "block";
menu2[0].style.display = "none" ;
menu2[1].style.display = "none" ;
menu2[2].style.display = "none" ;
};
createMenu();
Note: I tried to do the display block/none with a loop for less code, but it didn't seem to work at all, not even in jfiddle.
var createMenu = function()
{
var i = 0;
while (i <3 ) {
i++;
menu[i].style.display = "block";
menu2[i].style.display = "none" ;
}
};
Thanks for the help!
menu
doesn't exist when your code runs outside of jsfiddle - you need to run it on page load. – James Thorpe