Submit event of a submit type input isn't triggered












6














I wrote a silly example to see which of the two, the click or the submit event, fires first on an input of type submit.



When I run it, though, nothing is written to the console.



Stepping through the code in the debugger reveals the following series of occurrences.




  1. It fires the click event handler first, writes the text "click" to the console, but then shortly afterwards, the text disappears from the console.


  2. Also, the submit event is never fired.



I infer that the form is getting submitted after executing the click event of the button. The thing I don't understand is why it isn't getting into my submit event handler?



Below is the relevant code, and here is the full working example. It's just a simple HTML file you can download and try out on your machine.






(function() {
let btn = document.getElementById("btnSave");

btn.addEventListener("click", function(event) {
console.log("click");
}, false);
btn.addEventListener("submit", function(event) {
console.log("submit");
// event.preventDefault();
}, false);

console.log("All done now. Click da button, chum, click it I say!");

})();

div { margin: 20px; }
input[type="submit"] { width: 200px; height: 50px; }

<div>Look at the console and then click the button, chum!</div>

<div>
<form>
<input type="submit" id="btnSave" value="Save" width="200px" />
</form>
</div>












share|improve this question





























    6














    I wrote a silly example to see which of the two, the click or the submit event, fires first on an input of type submit.



    When I run it, though, nothing is written to the console.



    Stepping through the code in the debugger reveals the following series of occurrences.




    1. It fires the click event handler first, writes the text "click" to the console, but then shortly afterwards, the text disappears from the console.


    2. Also, the submit event is never fired.



    I infer that the form is getting submitted after executing the click event of the button. The thing I don't understand is why it isn't getting into my submit event handler?



    Below is the relevant code, and here is the full working example. It's just a simple HTML file you can download and try out on your machine.






    (function() {
    let btn = document.getElementById("btnSave");

    btn.addEventListener("click", function(event) {
    console.log("click");
    }, false);
    btn.addEventListener("submit", function(event) {
    console.log("submit");
    // event.preventDefault();
    }, false);

    console.log("All done now. Click da button, chum, click it I say!");

    })();

    div { margin: 20px; }
    input[type="submit"] { width: 200px; height: 50px; }

    <div>Look at the console and then click the button, chum!</div>

    <div>
    <form>
    <input type="submit" id="btnSave" value="Save" width="200px" />
    </form>
    </div>












    share|improve this question



























      6












      6








      6







      I wrote a silly example to see which of the two, the click or the submit event, fires first on an input of type submit.



      When I run it, though, nothing is written to the console.



      Stepping through the code in the debugger reveals the following series of occurrences.




      1. It fires the click event handler first, writes the text "click" to the console, but then shortly afterwards, the text disappears from the console.


      2. Also, the submit event is never fired.



      I infer that the form is getting submitted after executing the click event of the button. The thing I don't understand is why it isn't getting into my submit event handler?



      Below is the relevant code, and here is the full working example. It's just a simple HTML file you can download and try out on your machine.






      (function() {
      let btn = document.getElementById("btnSave");

      btn.addEventListener("click", function(event) {
      console.log("click");
      }, false);
      btn.addEventListener("submit", function(event) {
      console.log("submit");
      // event.preventDefault();
      }, false);

      console.log("All done now. Click da button, chum, click it I say!");

      })();

      div { margin: 20px; }
      input[type="submit"] { width: 200px; height: 50px; }

      <div>Look at the console and then click the button, chum!</div>

      <div>
      <form>
      <input type="submit" id="btnSave" value="Save" width="200px" />
      </form>
      </div>












      share|improve this question















      I wrote a silly example to see which of the two, the click or the submit event, fires first on an input of type submit.



      When I run it, though, nothing is written to the console.



      Stepping through the code in the debugger reveals the following series of occurrences.




      1. It fires the click event handler first, writes the text "click" to the console, but then shortly afterwards, the text disappears from the console.


      2. Also, the submit event is never fired.



      I infer that the form is getting submitted after executing the click event of the button. The thing I don't understand is why it isn't getting into my submit event handler?



      Below is the relevant code, and here is the full working example. It's just a simple HTML file you can download and try out on your machine.






      (function() {
      let btn = document.getElementById("btnSave");

      btn.addEventListener("click", function(event) {
      console.log("click");
      }, false);
      btn.addEventListener("submit", function(event) {
      console.log("submit");
      // event.preventDefault();
      }, false);

      console.log("All done now. Click da button, chum, click it I say!");

      })();

      div { margin: 20px; }
      input[type="submit"] { width: 200px; height: 50px; }

      <div>Look at the console and then click the button, chum!</div>

      <div>
      <form>
      <input type="submit" id="btnSave" value="Save" width="200px" />
      </form>
      </div>








      (function() {
      let btn = document.getElementById("btnSave");

      btn.addEventListener("click", function(event) {
      console.log("click");
      }, false);
      btn.addEventListener("submit", function(event) {
      console.log("submit");
      // event.preventDefault();
      }, false);

      console.log("All done now. Click da button, chum, click it I say!");

      })();

      div { margin: 20px; }
      input[type="submit"] { width: 200px; height: 50px; }

      <div>Look at the console and then click the button, chum!</div>

      <div>
      <form>
      <input type="submit" id="btnSave" value="Save" width="200px" />
      </form>
      </div>





      (function() {
      let btn = document.getElementById("btnSave");

      btn.addEventListener("click", function(event) {
      console.log("click");
      }, false);
      btn.addEventListener("submit", function(event) {
      console.log("submit");
      // event.preventDefault();
      }, false);

      console.log("All done now. Click da button, chum, click it I say!");

      })();

      div { margin: 20px; }
      input[type="submit"] { width: 200px; height: 50px; }

      <div>Look at the console and then click the button, chum!</div>

      <div>
      <form>
      <input type="submit" id="btnSave" value="Save" width="200px" />
      </form>
      </div>






      javascript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 20 mins ago









      JJJ

      29k147591




      29k147591










      asked 2 hours ago









      Water Cooler v2

      12.1k29100197




      12.1k29100197
























          1 Answer
          1






          active

          oldest

          votes


















          9















          The thing I don't understand is why it isn't getting into my submit event handler?




          input elements don't have a submit event. You need to hook submit on the form, not the input.



          btn.form.addEventListener("submit", function(event) {
          // ^^^^^
          console.log("submit");
          event.preventDefault();
          }, false);


          (btn.form there could be btn.closest("form") if you prefer, though btn.form is more widely-supported and is (also) standardized. Or, of course, use getElementById or querySelector to retrieve the form element.)



          Live Example:






          (function() {
          let btn = document.getElementById("btnSave");

          btn.addEventListener("click", function(event) {
          console.log("click");
          }, false);
          btn.form.addEventListener("submit", function(event) {
          // ^^^^^
          console.log("submit");
          event.preventDefault();
          }, false);

          console.log("All done now. Click da button, chum, click it I say!");

          })();

          div {
          margin: 20px;
          }

          input[type="submit"] {
          width: 200px;
          height: 50px;
          }

          <div>Look at the console and then click the button, chum!</div>

          <div>
          <form>
          <input type="submit" id="btnSave" value="Save" width="200px" />
          </form>
          </div>








          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967705%2fsubmit-event-of-a-submit-type-input-isnt-triggered%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            9















            The thing I don't understand is why it isn't getting into my submit event handler?




            input elements don't have a submit event. You need to hook submit on the form, not the input.



            btn.form.addEventListener("submit", function(event) {
            // ^^^^^
            console.log("submit");
            event.preventDefault();
            }, false);


            (btn.form there could be btn.closest("form") if you prefer, though btn.form is more widely-supported and is (also) standardized. Or, of course, use getElementById or querySelector to retrieve the form element.)



            Live Example:






            (function() {
            let btn = document.getElementById("btnSave");

            btn.addEventListener("click", function(event) {
            console.log("click");
            }, false);
            btn.form.addEventListener("submit", function(event) {
            // ^^^^^
            console.log("submit");
            event.preventDefault();
            }, false);

            console.log("All done now. Click da button, chum, click it I say!");

            })();

            div {
            margin: 20px;
            }

            input[type="submit"] {
            width: 200px;
            height: 50px;
            }

            <div>Look at the console and then click the button, chum!</div>

            <div>
            <form>
            <input type="submit" id="btnSave" value="Save" width="200px" />
            </form>
            </div>








            share|improve this answer




























              9















              The thing I don't understand is why it isn't getting into my submit event handler?




              input elements don't have a submit event. You need to hook submit on the form, not the input.



              btn.form.addEventListener("submit", function(event) {
              // ^^^^^
              console.log("submit");
              event.preventDefault();
              }, false);


              (btn.form there could be btn.closest("form") if you prefer, though btn.form is more widely-supported and is (also) standardized. Or, of course, use getElementById or querySelector to retrieve the form element.)



              Live Example:






              (function() {
              let btn = document.getElementById("btnSave");

              btn.addEventListener("click", function(event) {
              console.log("click");
              }, false);
              btn.form.addEventListener("submit", function(event) {
              // ^^^^^
              console.log("submit");
              event.preventDefault();
              }, false);

              console.log("All done now. Click da button, chum, click it I say!");

              })();

              div {
              margin: 20px;
              }

              input[type="submit"] {
              width: 200px;
              height: 50px;
              }

              <div>Look at the console and then click the button, chum!</div>

              <div>
              <form>
              <input type="submit" id="btnSave" value="Save" width="200px" />
              </form>
              </div>








              share|improve this answer


























                9












                9








                9







                The thing I don't understand is why it isn't getting into my submit event handler?




                input elements don't have a submit event. You need to hook submit on the form, not the input.



                btn.form.addEventListener("submit", function(event) {
                // ^^^^^
                console.log("submit");
                event.preventDefault();
                }, false);


                (btn.form there could be btn.closest("form") if you prefer, though btn.form is more widely-supported and is (also) standardized. Or, of course, use getElementById or querySelector to retrieve the form element.)



                Live Example:






                (function() {
                let btn = document.getElementById("btnSave");

                btn.addEventListener("click", function(event) {
                console.log("click");
                }, false);
                btn.form.addEventListener("submit", function(event) {
                // ^^^^^
                console.log("submit");
                event.preventDefault();
                }, false);

                console.log("All done now. Click da button, chum, click it I say!");

                })();

                div {
                margin: 20px;
                }

                input[type="submit"] {
                width: 200px;
                height: 50px;
                }

                <div>Look at the console and then click the button, chum!</div>

                <div>
                <form>
                <input type="submit" id="btnSave" value="Save" width="200px" />
                </form>
                </div>








                share|improve this answer















                The thing I don't understand is why it isn't getting into my submit event handler?




                input elements don't have a submit event. You need to hook submit on the form, not the input.



                btn.form.addEventListener("submit", function(event) {
                // ^^^^^
                console.log("submit");
                event.preventDefault();
                }, false);


                (btn.form there could be btn.closest("form") if you prefer, though btn.form is more widely-supported and is (also) standardized. Or, of course, use getElementById or querySelector to retrieve the form element.)



                Live Example:






                (function() {
                let btn = document.getElementById("btnSave");

                btn.addEventListener("click", function(event) {
                console.log("click");
                }, false);
                btn.form.addEventListener("submit", function(event) {
                // ^^^^^
                console.log("submit");
                event.preventDefault();
                }, false);

                console.log("All done now. Click da button, chum, click it I say!");

                })();

                div {
                margin: 20px;
                }

                input[type="submit"] {
                width: 200px;
                height: 50px;
                }

                <div>Look at the console and then click the button, chum!</div>

                <div>
                <form>
                <input type="submit" id="btnSave" value="Save" width="200px" />
                </form>
                </div>








                (function() {
                let btn = document.getElementById("btnSave");

                btn.addEventListener("click", function(event) {
                console.log("click");
                }, false);
                btn.form.addEventListener("submit", function(event) {
                // ^^^^^
                console.log("submit");
                event.preventDefault();
                }, false);

                console.log("All done now. Click da button, chum, click it I say!");

                })();

                div {
                margin: 20px;
                }

                input[type="submit"] {
                width: 200px;
                height: 50px;
                }

                <div>Look at the console and then click the button, chum!</div>

                <div>
                <form>
                <input type="submit" id="btnSave" value="Save" width="200px" />
                </form>
                </div>





                (function() {
                let btn = document.getElementById("btnSave");

                btn.addEventListener("click", function(event) {
                console.log("click");
                }, false);
                btn.form.addEventListener("submit", function(event) {
                // ^^^^^
                console.log("submit");
                event.preventDefault();
                }, false);

                console.log("All done now. Click da button, chum, click it I say!");

                })();

                div {
                margin: 20px;
                }

                input[type="submit"] {
                width: 200px;
                height: 50px;
                }

                <div>Look at the console and then click the button, chum!</div>

                <div>
                <form>
                <input type="submit" id="btnSave" value="Save" width="200px" />
                </form>
                </div>






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 1 hour ago

























                answered 2 hours ago









                T.J. Crowder

                676k12011961292




                676k12011961292






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53967705%2fsubmit-event-of-a-submit-type-input-isnt-triggered%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Accessing regular linux commands in Huawei's Dopra Linux

                    Can't connect RFCOMM socket: Host is down

                    Kernel panic - not syncing: Fatal Exception in Interrupt