I have a form on a page I want to submit using JavaScript:
<script>
function deleteFromlist(listname, listvalue)
{
if (confirm("Are you sure you want to delete this?")) {
document.getElementById('fromList').value=listname;
document.getElementById('deleteRecord').value=listvalue;
document.getElementById('watchlistForm').submit(); // Fails here
}
}
</script>
<form method='POST'
id='watchlistForm'
enctype='multipart/form-data'
action='process.php'>
<input type='text' name='fromList' id='fromList' value=''>
<input type='text' name='deleteRecord' id='deleteRecord' value=''>
<input type='submit' name='submit' value='submit'>
</form>
The JavaScript function is called from a href
in an table elsewhere on the page. It correctly populates the input fields, but it fails with this message:
TypeError: document.getElementById(...).submit is not a function
document.getElementById('watchlistForm').submit();
If I replace the doc....submit(); with
alert(document.getElementById('watchlistForm').innerHTML
then I get the expected output in the alert box. Hence
document.getElementById('watchlistForm')
does resolve to the form element.
Adding a submit button on the form works as expected.
Why is this not working and how can I fix it?
Sometimes the problem is caused by old versions of the Javascript files, cached by your browser and can be fixed by clearing the browser cache. You can use the browser console of your browser for debugging. After the Javascript error is fixed, the submit button will automatically be enabled.
The NUMBER ONE error is having ANYTHING with the reserved word submit as ID or NAME in your form. If you plan to call . submit() on the form AND the form has submit as id or name on any form element, then you need to rename that form element, since the form's submit method/handler is shadowed by the name/id attribute.
The method form. submit() is used for dynamic creation and sending the details to the server. The JavaScript form submission can be used for object creation and various attributes can also be used. The attributes can be class, id, tag, etc.
In javascript onclick event , you can use form. submit() method to submit form. You can perform submit action by, submit button, by clicking on hyperlink, button and image tag etc. You can also perform javascript form submission by form attributes like id, name, class, tag name as well.
Erk, it seems that the button named 'submit' was masking out the method. Changing:
input type='submit' name='submit' value='submit'
to
input type='submit' name='submitButton' value='submit'
solved the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With