Submit event of a submit type input isn't triggered

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












7














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




























    7














    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


























      7












      7








      7







      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 Dec 29 '18 at 9:46









      JJJ

      29k147591




      29k147591










      asked Dec 29 '18 at 7:46









      Water Cooler v2Water Cooler v2

      12.2k29101198




      12.2k29101198






















          1 Answer
          1






          active

          oldest

          votes


















          10















          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









            10















            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



























              10















              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

























                10












                10








                10







                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 Dec 29 '18 at 8:13

























                answered Dec 29 '18 at 7:51









                T.J. CrowderT.J. Crowder

                679k12112011296




                679k12112011296



























                    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.




                    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

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?