why does this work..
if(document.addEventListener){
myForm.addEventListener('submit',myAlert(),false);
}else{
myForm.attachEvent('onsubmit',myAlert());
}
using myAlert() here is giving the returned value of the function, not the function itself.
Consider this:
function bob() {
return "hello world";
}
alert(bob());
the alert is going to use the value returned by the function bob.
If you want to pass additional parameters in you addEventListener, try this:
if(document.addEventListener){
myForm.addEventListener('submit',function(event) {
myAlert(event,myOtherParamater);
},false);
}else{
myForm.attachEvent('onsubmit',function () {
myAlert(window.event,myOtherParameter);
});
}
javascript uses first-class functions and can thus be passed as parameters to functions which is what the addEventListener and attachEvent methods are expecting. Hope that helps.