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.
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.
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.
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.
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.
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'; })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With