0

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
1
  • Now it is answerable. =] Commented Aug 3, 2013 at 19:14

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.