Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addEventListener on form submit [closed]

This is what I have and it's not working.

document.getElementById('form1').addEventListener('submit', function(){     document.getElementById('donate').style.display = 'none';     document.getElementById('topMessage').style.display = 'none'; }); 

The javascript console shows this error:

Uncaught TypeError: Cannot set property 'onclick' of undefined

Any suggestion is much appreciated.

like image 228
2myCharlie Avatar asked Jan 24 '14 17:01

2myCharlie


People also ask

What event fires when a form is submitted?

The submit event fires when the user clicks a submit button ( <button> or <input type="submit">) or presses Enter while editing a field (e.g. <input type="text">) in a form. The event is not sent to the form when calling the form. submit() method directly.

How do I stop page reload on Submit?

Use the preventDefault() method on the event object to prevent a page refresh on form submit in React, e.g. event. preventDefault() . The preventDefault method prevents the browser from issuing the default action which in the case of a form submission is to refresh the page.

How do I listen a submit on an event?

to listen to the form submit event and get the data from the form with the FormData constructor. We get the form with document. querySelector . Then we call addEventListener on it with 'submit' as the first argument.

What happens when submit button is clicked?

The form will be submitted to the server and the browser will redirect away to the current address of the browser and append as query string parameters the values of the input fields.


1 Answers

Okay, I figured it out. All I need the preventDefault(); So, here's the solution.

document.getElementById('form1').addEventListener('submit', function(evt){     evt.preventDefault();     document.getElementById('donate').style.display = 'none';     document.getElementById('topMessage').style.display = 'none'; }) 
like image 168
2myCharlie Avatar answered Oct 05 '22 23:10

2myCharlie