I'm having trouble with an object:
var page = document.getElementById('example');
var p1_box = {
x: 20,
y: 20,
width: 560,
height: 400
};
page.innerHTML = (p1_box.x);
when using the above code, the page shows 20 as it should, however when I use this code:
var page = document.getElementById('example');
var p1_box = {
x: 20,
y: 20,
width: 560,
height: 400
};
function test(){
page.innerHTML = (p1_box.x);
}
(and then run the test function) it doesn't work. And I instead get this error (in the Chrome dev console:)
Uncaught TypeError: Cannot read property 'x' of undefined
anyone know what I am doing wrong? As far as I can tell there is nothing wrong with this code, and yet as I am being shown, it is not working.
[I included the 'scope' tag with this question since I thought it might have something to do with that]
EDIT: I was reluctant to provide the full code, but it appears I will. The problem may just lie in how messy it is currently. I copied the code from a game I made JavaScript and am making a new one. So if you are wondering why so much of the code is commented out, that would be why. Hopefully with this the problem will become obvious.
First off: here's the webpage where it loads: http://oldforgeinn.ddns.net/games/?game=battleship and here's the source code: http://oldforgeinn.ddns.net/scripts/SO_file.js
And a reminder, that the code blocks above are examples, and the function/variable names don't match the actual ones in my code.
in this case test() would represent the draw_gui() function, and the page variable doesn't matter, as the point of this question was why x is undefined, and the innerHTML part is just to visibly confirm.