The onsubmit form does not work with the .submit () form

I have a form and I applied the onsubmit function:

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

I added a submit button to my form <input type="submit" value="submit"/>

Text is being somethingwarned. Fine!

I have an image and I want to send it to the form:

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

But this does not make the function ... it does not warn somethingon the screen.

EDIT: I don't want something with jQuery. Pure js

+4
source share
3 answers

The problem is that the event handlers do not respond to the event raised by the script.

Say as below

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

And manually call the onclickimage function as below:

<img onclick="FormSubmit();document.getElementById('myform').submit();" src="mylocation"/>
+2
source

, , form.submit(). , , form.submit(), , .

, , :

function mySubmit() {
    alert("something");
}

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

:

<img onclick="mySubmit(); document.getElementById('myform').submit();" src="mylocation"/>
+3

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

<a href="javascript://" onClick="document.getElementById('myform').submit();">
  <img src="mylocation" />
</a>
0
source

All Articles