+ 1
Why are we using "return" on "onsubmit"?
https://code.sololearn.com/WA25A1012a22 This is the code for form validation in JS. Can anyone explain why are we using return on "onsubmit" event? The function already returns the value.
7 Answers
+ 3
NBinte
after evaluation the function spits either true or false which just evaluates to something like this
onsubmit=true or onsubmit=false.
but in truth onsubmit takes a function callback (after learning JavaScript you will know it)..
so you need to return something ... not set the callback event to true or false ..
now get it?
I believe you should see for callbacks and events to get a better understanding ...
have a look at these : https://www.w3schools.com/js/js_callback.asp
https://www.w3schools.com/jsref/event_onsubmit.asp
+ 2
actually you place a JavaScript code to run in onsubmit attribute of form.
and if the value returned is true , then only form gets submitted successfully , else its not.
Hope you got it !?đ
+ 2
Okay get it. Thanks everyone. :)
+ 1
@Prashanth Calling the function like onsubmit="validate()" already gets the returned the value and it works too. My question was why we are again using onsubmit="return validate()".
+ 1
NBinte
Check with and without return and enter different values in both fields then submit you will know what Prashanth Kumar wanted to say.
+ 1
NBinte
If you do not use return then after returning false form will be submit.
+ 1