What is the difference between:
<div onclick="return function()"></div>
vs
<div onclick="function()"></div>
They seem to be doing the same thing for me and I am not sure which one to use.
Take for example <form onsubmit="return validate();">...
The submit will be cancelled if validate() returns false.
With <form onsubmit="validate();">...
The submit will continue regardless of the return value of validate().
One will return the value to the element which the attribute resides in, the other will not.
When you use return function ()
the default click-method of the element will only be executed if function ()
evaluates to a value that implicitly is converted to true.
If it yields false the evaluation of the click-event will halt after running the onclick-property.
In case of onclick="function ()"
the default click-propery of the element will always be executed, no matter what the function returns.
function some_func () { return false; }
<a href="http://google.com" onclick="return some_func()">
link #1
</a> <!-- user will never end up at google -->
if javascript is enabled in the browser, of course..
<a href="http://google.com" onclick="some_func()">
link #1
</a> <!-- user will always end up at google -->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With