1

I have a form and on clicking the image submit form is triggered by default. I am not understanding why click is triggering form submit. Can someone please guide on this?

<form name="test1" action="er" method="post" onsubmit="return validateForm()"
<input type="text" id="du" name="du" value="74491" onkeyup="showButton()">
<input type="image" src="../images/save-icon.png" width="20" height="20" ></form>
Pran
  • 153
  • 1
  • 12

3 Answers3

5

This is the expected behavior. According to the standard:

input elements of type image are used to create graphical submit buttons, i.e. submit buttons that take the form of an image rather than text.

Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26
2

An input type="image" only defines that image as the submit button. For more check this one

nagiatzi
  • 106
  • 5
1

Please use <img src=''>

instead of <input type='image'>.

Because input type image fires a default submit action! Otherwise here is a sample script that stops form submitting whenever you click a image input.

Hope my code will help you!

document.addEventListener("click", function(e){
  const path=e.path;
  for(let i=0;i<path.length-4;i++){
    if(path[i].tagName=="INPUT"&&path[i].type=="image"){
      var form=path[i].parentNode;
      while(form.tagName!="FORM") form=form.parentNode;
      
      let currentSubmitAttr=form.getAttribute("onsubmit");
      
      form.setAttribute("onsubmit", "return false;");
      setTimeout(function(){
        form.setAttribute("onsubmit", currentSubmitAttr);
      }, 100);
    }
  }
  return false;
});
<form action="action.php">
  <input type="text" name="du">
  <br><br>
  <input type='image' src='https://dummyimage.com/300x250/000/fff'>
</form>
Adnane Ar
  • 683
  • 7
  • 11