Trying to cancel the sending of an empty form. Confused as there seems to be no reason why this doesnt work. Using firefox as my browser.
Could anybody explain why this code does not cancel the form being sent yet it fires the alert dialog.
addEvent(searchForm, "onsubmit", function(){
if(inputBox.value.trim() === ""){
alert("empty"); //this line gets called
return false; //this doesn't
}
});
Many Thanks
this is the addEvent function
function addEvent(element, listener, func){
if(element.addEventListener){
listener = listener.substr(2, listener.length);
element.addEventListener(listener, func);
} else {
element.attachListener(listener, func);
}
}
asked Aug 3, 2013 at 19:04
user866190
8635 gold badges15 silver badges31 bronze badges
2 Answers 2
your handler should be
function(e){
if(inputBox.value.trim() == ""){
alert('empty');
e.preventDefault();
}
}
Normally regardless of the framework used handlers support passing the event itself as an argument.
answered Aug 3, 2013 at 19:07
Joe Minichino
2,76524 silver badges20 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
addEvent is not a native javascript function.
use this instead..
searchForm.onsubmit = function(e){
if(inputBox.value.trim() === ""){
alert("empty");
e.preventDefault();
return false;
}
}
answered Aug 3, 2013 at 19:10
Hemant_Negi
2,1381 gold badge22 silver badges25 bronze badges
Comments
lang-js
=]