I am trying to sharpen my vanilla JavaScript skills a little bit. I am working on a form validator just for fun. Here is my code thus far:
var getParent = document.getElementById("myForm");
document.getElementById("submit").onclick = function(e) {
e.preventDefault();
var cache = !cache ? "Nothing has been selected" : cache;
for(i = 0; i < getParent.elements.method.length; i++) {
if(getParent.elements.method[i].checked) {
cache = getParent.elements.method[i].value;
}
}
getParent.submit();
}
As you can see, this just tests some radio buttons to see if they are checked or not. I am trying to use the .submit() function at the bottom to submit my form, but I am getting an error. Why is this code not submitting my form with .submit()??
You can not have the button named
submitand usesubmit().The button will override the method. So, when you call
getParent.submit()it actually points to the button, not the actual submit function.