addEventListener on form submit

2myCharlie picture 2myCharlie · Jan 24, 2014 · Viewed 88.6k times · Source

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.

Answer

2myCharlie picture 2myCharlie · Jan 24, 2014

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';
})