Skip to content Skip to sidebar Skip to footer

Form Onsubmit Doesn't Work With Form .submit()

I have a form and I have applied a function onsubmit: var theForm = document.getElementById('myform'); theForm.onsubmit = function FormSubmit() { alert('something'); }; I have add

Solution 1:

Event handlers are only run in response to user actions or asynchronous events, not normal program actions like calling form.submit(). One reason for this is that it's not uncommon for the submit handler of a form to do some work and then call form.submit(), and this would cause an infinite recursion if it triggered the handler again.

Put the code you want to run in a named function:

functionmySubmit() {
    alert("something");
}

theForm.onsubmit = function() {
    mySubmit();
};

Then you can call that function in your click handler:

<imgonclick="mySubmit(); document.getElementById('myform').submit();"src="mylocation"/>

Solution 2:

The problem is event handlers doesn't respond when the event called by script.

Say like bellow

var theForm = document.getElementById("myform");
  functionFormSubmit() {
    alert("something");
  };
  theForm.onsubmit = FormSubmit;

And call manually the function onclick of image like bellow

<imgonclick="FormSubmit();document.getElementById('myform').submit();"src="mylocation"/>

Solution 3:

You can use an a tag around your image, something like this:

<ahref="javascript://"onClick="document.getElementById('myform').submit();"><imgsrc="mylocation" /></a>

Post a Comment for "Form Onsubmit Doesn't Work With Form .submit()"