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>
11 Jan 2020 at 12:41 PM
0pnshow more
StorStor
Follow us on find us on facebook follow us on twitter
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>
16 Feb 2020 at 03:54 PM
0pnshow more
TimTim
Share on FacebookShare on TwitterShare on LinkedInSend email
2021 AnswerTabsTermsContact us