0
var submit = document.getElementById('submit'),
    div = document.getElementById('div');

div.onmouseover = function sum() {
    value1 = document.getElementById('form').value;
    value1 = document.getElementById('form2').value;

    sum = value1 + value2;
}

submit.onsubmit = function print() {
    div.innerHTML = sum;
}

For some reason, the code doesn't work; however, if I replace the last with

submit.onclick = function print() {
    div.innerHTML = sum;
}

onclick instead of onsubmit, it works perfectly fine, even though sum was calculated outside and before I press submit. It's as if every variable becomes null when I enter submit even though the variables aren't even stored in the form fields.

1
  • What kind of element is document.getElementById('submit')? submit events are only triggered on form elements. Commented Feb 28, 2013 at 21:20

2 Answers 2

1

The onsubmit event handler must be attached to the form element, not to input element.

Assuming you have a form, you could do

submit.form.onsubmit = ...
Sign up to request clarification or add additional context in comments.

2 Comments

@Matthew: "it doesn't work" is not a helpful problem or error description. My toaster doesn't work either. Please be more precise, then we can provide better help.
And the HTML would be useful so that we don't have to guess. In fact a fiddle demonstrating the problem would probably ensure a good solution is given.
0

The onsubmit event fires on the form element, not the button. Try moving the onsubmit handler to the form instead

2 Comments

i tried form.form.onsubmit and it still doesn't work. it works if i write submit.onclick though.
submit.form.onsubmit doesn't work; submit.onsubmit doesn't work

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.