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?

2
  • do you have this error on all browsers or any specific browsers? Commented Apr 23, 2019 at 18:27
  • 1
    None of the code you've posted so far would produce that error. Please update your question with a minimal reproducible example Commented Apr 23, 2019 at 18:28

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much! it works perfectly now. @Unamata Sanatarai
You could improve this answer by quoting from specifications (like the green box text on 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.