1
//html code for form
<form id="contactform" method="post" action="/action_page.php">
//submit button html code
<input type="button" id="submit" onclick="submit_form()" value="Submit">
//javascript submit function.
function submit_form()
{
document.getElementById("contactform").submit();
return false;
}

so here are the html and javascript snippets of my contact form, now when I am debugging my javascript code it is giving me a member not found error at the document.getElementById("contactform").submit(); line. The problem still persists even though the id of my button and the name of the function that runs are different. Can somebody please help me resolve this?

Bisma
  • 159
  • 1
  • 1
  • 12

1 Answers1

2

The problem was your input button had an ID of submit.

I renamed that id and you can see below a working code:

function submit_form()
{
document.getElementById("contactform").submit()
return false;
}
<form id="contactform" method="post" action="/action_page.php">

<input type="button" id="hsubmit" onclick="submit_form()" value="Submit">
</form>
Unamata Sanatarai
  • 6,475
  • 3
  • 29
  • 51
  • thank you so much! it works perfectly now. @Unamata Sanatarai – Bisma Apr 23 '19 at 18:33
  • 2
    You could improve this answer by quoting from specifications (like the green box text on https://www.w3.org/TR/html50/forms.html#the-form-element) that dictate that the form element gets dynamic attributes by the name or ID of its input controls. – trincot Apr 23 '19 at 18:38