Javascript return false - from function
Is there a way to do the following:
validateLogin();
return false;
But actually like this ..
validateLogin();
And here is the function:
function validateLogin(){
if(hi=true){
return true;
}
else{
return false
}
I want to attach this function for the form submission case, so if the HI is false - I want to return false; this means it will stop to submit the form. I know it returns false; get it done, I just need to know how can I revert this back to the parent function?
Thanks in advance!
source to share
I am using the following to stop events ...
event.returnValue = false;
cresentfresh inspired me to do some research ... here is an article with a compatibility matrix.
There are also related threads in SO .
source to share
Ultimately you can do this:
return validateLogin();
Note that your function code has some bugs (perhaps due to the simplification of the code you did to post this question?). You'd better write this method like this:
function validateLogin(){
...
return hi;
}
Please note that on condition if (hi=true) {
you must write if (hi == true) {
or better if (hi) {
...
source to share
The standard way to stop the default action for an event is:
event. preventDefault();
You can also prevent events from propagating with event.stopPropgation (); to stop the event listeners.
http://www.w3.org/TR/2001/WD-DOM-Level-3-Events-20010823/events.html#Events-Event
However IE doesn't recognize this, so you can set event.returnValue to false for IE.
eg:
if (event && event.preventDefault) event.preventDefault();
event.returnValue = false;
You can also return false from an event handler for HTML related events.
<form onsubmit="return validateLogin()">
However, this is not considered best practice.
Note. The event object is passed as the first argument in your event listener.
eg:
function validateLogin(e) {
e; // is the event object
}
For IE, you might need window.event.
function validateLogin(e) {
e = e || window.event; // is the event object
}
source to share