Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript 'onclick' event 'return' keyword functionality

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!

like image 521
Shiroi98 Avatar asked Oct 18 '11 23:10

Shiroi98


People also ask

What does Onclick return in JavaScript?

Means it will keep you on the same page if the function returns false and you can fill up the value into the field again.

How do you write a function on onclick event?

Just invoke the code you're trying to invoke: onclick="alert('hello')" If you want to define a function, do that separately in your JavaScript code and just invoke the function in onclick . (Or, even better, attach it as a handler from the JavaScript code so you're not writing in-line JavaScript in your HTML.)

Can onclick events have two functions?

Greetings! Yes, you can call two JS Function on one onClick.

What is return false in Onclick?

using return false in an onclick event stops the browser from processing the rest of the execution stack, which includes following the link in the href attribute. In other words, adding return false stops the href from working. In your example, this is exactly what you want.


2 Answers

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/

like image 37
Yaakov Shoham Avatar answered Sep 24 '22 05:09

Yaakov Shoham


Some html elements have JS events that behave differently when true/false is returned. For instance:

<input type='submit' value='Click Me' onSubmit='ValidateForm();'> 

...vs...

<input type='submit' value='Click Me' onSubmit='return ValidateForm();'> 

In the second instance, if the ValidateForm function returned false the form will not submit, in the first even if the function returns false the form will still submit.

I think this scenario, you can see the different between using the return keyword and not.

UPDATED To simplify, if you use the return keyword you are passing a value back to the function that called the onsubmit. Without it, you are simply calling the function that you name in the event handler and do not return anything.

UPDATE 2021-01-21 This functionality also work for the onclick method on html anchors / links (a):

Sample Usage:

<a href="#never-used" onclick="alert('click clack'); return false;" >  

Click Me

like image 121
Glenn Ferrie Avatar answered Sep 25 '22 05:09

Glenn Ferrie