Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
22
rated 0 times [  26] [ 4]  / answers: 1 / hits: 122807  / 13 Years ago, tue, october 18, 2011, 12:00:00

I am really new to javascript, and stumbled upon the return keyword. Basically, what is the difference in terms of these 2 statements?



<input type=checkbox onclick=doAlert() />


vs



<input type=checkbox onclick=return doAlert(); />


Essentially, both returned the same results and called the function, but is there more to it? Any help greatly appreciated :). Thanks!


More From » html

 Answers
9

Returning false from the function, will abort the effect of the checking. Because the native of functions that written hardcoded into html properties (it became some new local function), writing the html without the word return will just run the function, and lose its returning value, as if you've wrote:



function doAlert() {
if(some_condition)
return false;
else
return true;
}
function some_local_function() {
doAlert();
}


Function some_local_function won't return any value, although doAlert returns.



When you write return, it's like you wrote the second function like this:



function some_local_function() {
return doAlert();
}


which preserves the returning value of doAlert, whatever it will be. If it's true - the action will perform (the checkbox will be checked) - otherwise - it will cancel.



You can see live expamle here: http://jsfiddle.net/RaBfM/1/


[#89543] Monday, October 17, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cullenmaxl

Total Points: 414
Total Questions: 112
Total Answers: 87

Location: United States Minor Outlying Island
Member since Sat, May 28, 2022
2 Years ago
;