Reputation: 26556
Is there any 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 event of a form being submitted, so if HI is false - i want to return false; meaning it will stop for form from submitting.. i know return false; will do so i just need to know how i can get this return back to the parent function?
Thanks in advance!
Upvotes: 2
Views: 21788
Reputation: 10802
I use 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 on SO.
Upvotes: 3
Reputation: 5295
The standard way of stoping the default action of an event is:
event. preventDefault();
You may also want to prevent event propagation with event.stopPropgation(); to stop further event listeners from executing.
http://www.w3.org/TR/2001/WD-DOM-Level-3-Events-20010823/events.html#Events-Event
However, IE will not recognize this which is why 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 the event handler for events inlined in HTML.
<form onsubmit="return validateLogin()">
This is not considered best practice however.
Note: the event object is passed in as the first argument in your event listener.
eg:
function validateLogin(e) {
e; // is the event object
}
For IE you may need window.event.
function validateLogin(e) {
e = e || window.event; // is the event object
}
Upvotes: 2
Reputation: 35895
function validateLogin() {
return hi;
}
<form onsubmit="return validateLogin()">
...
</form>
Upvotes: 0
Reputation: 81627
You can eventually do that:
return validateLogin();
Note that your function code has some errors (maybe due to the simplification of the code you made to post this question?). You'd better write this method like that:
function validateLogin(){
...
return hi;
}
Note also that insted of having if (hi=true) {
, you must write if (hi == true) {
, or better if (hi) {
...
Upvotes: 3
Reputation: 38852
You can use it as follow:
return validateLogin();
however, as mmayo pointed out, don't forget about the return value:
event.returnValue = false;
Upvotes: 6