I have a form that posts to the page it os on and I would like to use some javascript that will make the page maintain it's scroll position when the form submits. The script that I have uses onsubmit but I already have another script using that.
Is it possible to use onsubmit() for more than one script eg..
onsubmit="return validateForm(), return saveScroll()"
Thanks
I have a form that posts to the page it os on and I would like to use some javascript that will make the page maintain it's scroll position when the form submits. The script that I have uses onsubmit but I already have another script using that.
Is it possible to use onsubmit() for more than one script eg..
onsubmit="return validateForm(), return saveScroll()"
Thanks
Share Improve this question asked Jan 20, 2013 at 18:59 tatty27tatty27 1,5545 gold badges37 silver badges74 bronze badges4 Answers
Reset to default 4Or you could do
onsubmit="validateForm();saveScroll(); return false;"
You can simply call both:
onsubmit="return validateForm() && saveScroll()"
Using &&
the form will only submit if both functions return true. You can use other logical operators to get the desired return value.
You can call any other functions you want from the first function
Eg. call saveScroll()
from validateForm()
Or you could simply seperate the calls by a ;
and then both will be executed.
A form can have only one onsubmit
attribute.
The value of that attribute is the body of a function, so if you return from a statement in it, then you will end the function instead of continuing to the next statement.
Thus, to run both functions with an onsubmit
attribute:
onsubmit="validateForm(); return saveScroll()"
You might want to change the logic there depending on how you want to react to the function calls. If the return value of validateForm
matters, then you should do something with it.