Your code is working fine since you included it in your question. You can see it here: http://jsfiddle.net/jfriend00/kXrDF/ .
What does this mean that there is something else wrong that you did not include in your question. Perhaps you can include more of your code so that we can help you find something else that might cause it.
To check the scope of your "global" variables, you can set a breakpoint in the add () function and check the values โโof number1, number2 and numAnswer. If you have not yet figured out how to set a breakpoint and check variables, I would highly recommend doing this. If you still cannot, you can put temporary tests in your add () code, like this, to narrow down what the problem is:
if (!number1) alert("number1 is not valid"); if (!number2) alert("number2 is not valid"); if (!numAnswer) alert("numAnswer is not valid");
Another possibility is that your code starts too early before the page loads, in which case the source code cannot find the DOM elements, because they do not exist yet. So that this is not a problem, you need to make sure that your code does not run until the page loads. You can do this by placing your code after the page elements or using one of the various detection methods when the page has loaded and does not run your code, which until then has initialized your global variables. Frameworks such as jQuery and YUI have built-in functions to help you run code after the page loads.
source share