I am trying to submit a form through onclick event on tag. I have tried triggering document.myform.submit(), this.form.submit(), parentNode.submit() etc. but none of this i
<form id="myform_id" action="/myMethode" role="form" method="post" >
<a href="javascript:$('#myform_id').submit();" >submit</a>
</form>
If jquery is allowed then you can use following code to implement it in the easiest way as :
<a href="javascript:$('#form_id').submit();">Login</a>
or
<a href="javascript:$('form').submit()">Login</a>
You can use following line in your head tag () to import jquery into your code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
I suggest to use "return false" instead of useing some javascript in the href-tag:
<form id="">
...
</form>
<a href="#" onclick="document.getElementById('myform').submit(); return false;">send form</a>
you can do it like this with raw javascript
<html>
<body>
<form id="my_form" method="post" action="mailto://test@test.com">
<a href="javascript:{}" onclick="document.getElementById('my_form').submit();">submit</a>
</form>
</body>
</html>
For those asking why I have a href
element in my anchor tag, its because it's a compulsory part of an anchor tag, it may well work without but it's not to spec. So if you want to guarantee rendering etc you must give the engine a fully formed tag. So the above achieves this. You will see href="#"
used sometimes but this is not always wanted as the browser will change the page position.
Submit your form like this ...
Your HTML code
<form name="myform" action="handle-data.php"> Search: <input type='text' name='query' />
<a href="javascript: submitform()">Search</a>
</form>
JavaScript Code
<script type="text/javascript">
function submitform() { document.myform.submit(); }
</script>