onSubmit return false not working - submit form by Javascript

I need to submit the form by using Javascript, but it seems the functions in onSubmit event don't work. When I click on Send link, the form is submitted, but I will not stay on the same page. It will redirect me to web page included in action attribute. Could you please tell me why is it not working ?

<form id="idmessage" name="message" method="post" onSubmit="javascript: alert('Form submitted'); return false;" action="index.php" >
<textarea name="content" ><textarea>
<br />
<a href="javascript:void(0);" onClick="javascript: document.getElementById('idmessage').submit(); " >Send</a>
</form>
posted on 11.01.2020 at 12:41
0pnshow more
StorStor
Hi,

The functions included in onSubmit event don't work, when you are using javascript function .submit() to submit the form. You have to call required javascript functions before document.getElementById('idmessage').submit();

For example:

<form id="idmessage" name="message" method="post" action="index.php" >
<textarea name="content" ><textarea>
<br />
<a href="javascript:void(0);" onClick="javascript: var valid = formValidation(); if(valid=='yes'){ alert('Form submitted'); document.getElementById('idmessage').submit(); }else { alert('Form not submitted'); } " >Send</a>
</form>
posted on 16.02.2020 at 15:54
0pnshow more
TimTim
share on facebookshare on twitter
2020 AnswerTabsTermsContact us
This site uses cookies to improve your experience, for analytics and ads. By continuing to browse this site you agree to our use of cookies.