Matthew Loch
Matthew Loch

Reputation: 59

onsubmit makes text printed using innerHTML disappears

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

So I write out something on div when I click on the button submit; however, everything disappears immediately after it appears. Why is that and is there a quick fix to that problem?

Upvotes: 0

Views: 793

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191749

The form is submitting so the page is reloading. The simplest way is:

submit.form.onsubmit = function () {
    div.innerHTML = sum;
    return false;
}

However, this prevents the form from actually submitting, which is something you may want to do. Perhaps you can submit the form with ajax while you update the div on the front end.

Alternatively (and preferably) use:

submit.form.addEventListener('submit', function (e) {
    div.innerHTML = sum;
    e.preventDefault();
});

to bind the event.

Upvotes: 1

Related Questions