There are more bugs then code in your code :-)
Nothing in your HTML calls any functions.
First, fatal bugs: nothing calls
calculate
. You need to add
id
to your button, say
id="button"
and then you could write, in simplest case:
var button = document.getElementById("button");
button.onclick = calculate;
Another bug is assignment to
innerHTML
in your last line. It should be
val
instead.
After these two fixes, the script will work immediately. After that, I suggest you rewrite the code the way in some reasonable ways. First of all, you don't need submit input; it is only needed for forms, only if you have the server part and want to perform HTTP request. In your pure-JavaScript code, use a usual
button
.
Don't use objects obtained via
document.getElementById
immediately. Return the objects and keep then for reuse. Initialize controls with some initial values.
Your "total" field doesn't have to be
input
. It's confusing that one actually can enter some data in it. Consider using any read-only element, such as
p
,
div
,
span
— anything (and than you would really use
innerHTML
). Or at least make it read-only input. Note that in desktop applications such "display-only" read-only input text boxes can be important to allow the users to put their content in the clipboard, but with HTML it's always possible for other elements, not only input ones.
—SA