2

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.

17
  • 2
    how/where/when do you run test()? Commented Feb 26, 2018 at 23:55
  • I have tried both onload (for an element) and just doing test(); at the bottom of the code, though I can assure you the function is being run since I'm getting the error in chrome. Commented Feb 26, 2018 at 23:59
  • 1
    neighet the html, nor the js file contain a test function, and you code throws an error that inputfield.getElementById is not a function Commented Feb 27, 2018 at 0:09
  • 2
    The problem is, your p1_box never gets to be defined due to that error. So fix that first! document.getElementById("input-field"), since it’s an HTMLElement, doesn’t have a getElementById method. Just use document.getElementById, not inputfield.getElementById. Use the browser console (dev tools) (hit F12) and read any errors. Please learn to use the debugger. Commented Feb 27, 2018 at 0:18
  • 1
    IDs are supposed to be unique throughout the whole page Commented Feb 27, 2018 at 0:20

1 Answer 1

4

That code works fine. There must be something else that you're doing wrong in the rest of your code. Here's a snippet of it working:

var page = document.getElementById('example');
var p1_box = {
    x: 20,
    y: 20,
    width: 560,
    height: 400
};
function test(){
    page.innerHTML = (p1_box.x);
}

test();
<div id="example"></div>

Sign up to request clarification or add additional context in comments.

8 Comments

I have seen another thing (I don't remember what it was called) where it would go something like this: var thing = 0; function test(){ {do something with thing variable} var thing; } and it would return undefined due to it being defined in that function, but coming later. I checked though and this is not the case.
@Magicrafter13Gaming there is nothing wrong with the code as you have posted it above. Your comment does not help with your problem. You need to include the rest of your code and how you are calling test()
Are you referring to variable hoisting?
Yeah, hoisting.
@Magicrafter13Gaming If you have a var p1_box in your test function, remove it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.