Sethen
Sethen

Reputation: 11348

.submit() and form validation in JavaScript

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()??

Upvotes: 2

Views: 211

Answers (1)

ATOzTOA
ATOzTOA

Reputation: 35980

You can not have the button named submit and use submit().

The button will override the method. So, when you call getParent.submit() it actually points to the button, not the actual submit function.

Upvotes: 4

Related Questions