Binding data-dismiss=“modal” to a key click










0















I have a simple modal that uses buttons to close it. The data-dismiss="modal" is binding to the button and it works perfectly fine.



I would like to bind the same functionality to my escape key and I'm wondering if its possible.



I tried something like the below but it does not work.



 $(document).keyup(function (e) 

if (e.which == 27)
$('#postcard-items-modal').modal('data-dismiss');


);


If anyone can shed some light on this that would be great.



I know I can add classes to hide the model or add a style tag to close it but im specifically looking to bind the data-dismiss to run when escape is pressed.



Thanks










share|improve this question






















  • Possible duplicate question, answer can be found, here stackoverflow.com/questions/12630156/…

    – cmprogram
    Nov 14 '18 at 17:18











  • Thank you, that worked for me

    – Davabo
    Nov 15 '18 at 9:38















0















I have a simple modal that uses buttons to close it. The data-dismiss="modal" is binding to the button and it works perfectly fine.



I would like to bind the same functionality to my escape key and I'm wondering if its possible.



I tried something like the below but it does not work.



 $(document).keyup(function (e) 

if (e.which == 27)
$('#postcard-items-modal').modal('data-dismiss');


);


If anyone can shed some light on this that would be great.



I know I can add classes to hide the model or add a style tag to close it but im specifically looking to bind the data-dismiss to run when escape is pressed.



Thanks










share|improve this question






















  • Possible duplicate question, answer can be found, here stackoverflow.com/questions/12630156/…

    – cmprogram
    Nov 14 '18 at 17:18











  • Thank you, that worked for me

    – Davabo
    Nov 15 '18 at 9:38













0












0








0








I have a simple modal that uses buttons to close it. The data-dismiss="modal" is binding to the button and it works perfectly fine.



I would like to bind the same functionality to my escape key and I'm wondering if its possible.



I tried something like the below but it does not work.



 $(document).keyup(function (e) 

if (e.which == 27)
$('#postcard-items-modal').modal('data-dismiss');


);


If anyone can shed some light on this that would be great.



I know I can add classes to hide the model or add a style tag to close it but im specifically looking to bind the data-dismiss to run when escape is pressed.



Thanks










share|improve this question














I have a simple modal that uses buttons to close it. The data-dismiss="modal" is binding to the button and it works perfectly fine.



I would like to bind the same functionality to my escape key and I'm wondering if its possible.



I tried something like the below but it does not work.



 $(document).keyup(function (e) 

if (e.which == 27)
$('#postcard-items-modal').modal('data-dismiss');


);


If anyone can shed some light on this that would be great.



I know I can add classes to hide the model or add a style tag to close it but im specifically looking to bind the data-dismiss to run when escape is pressed.



Thanks







javascript jquery bootstrap-modal






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 17:14









DavaboDavabo

60110




60110












  • Possible duplicate question, answer can be found, here stackoverflow.com/questions/12630156/…

    – cmprogram
    Nov 14 '18 at 17:18











  • Thank you, that worked for me

    – Davabo
    Nov 15 '18 at 9:38

















  • Possible duplicate question, answer can be found, here stackoverflow.com/questions/12630156/…

    – cmprogram
    Nov 14 '18 at 17:18











  • Thank you, that worked for me

    – Davabo
    Nov 15 '18 at 9:38
















Possible duplicate question, answer can be found, here stackoverflow.com/questions/12630156/…

– cmprogram
Nov 14 '18 at 17:18





Possible duplicate question, answer can be found, here stackoverflow.com/questions/12630156/…

– cmprogram
Nov 14 '18 at 17:18













Thank you, that worked for me

– Davabo
Nov 15 '18 at 9:38





Thank you, that worked for me

– Davabo
Nov 15 '18 at 9:38












2 Answers
2






active

oldest

votes


















1














The Answer:



So I didn't need any javascript to bind a keyclick.



Add I had to do was add tabindex='-1' to my modal id



<div class="modal fade" role="dialog" id="PostcardModal" tabindex='-1'></div>


This will enable the escape button to close the modal.



Thank you to cmprogram.






share|improve this answer






























    0














    Have you tried e.keyCode instead of e.which? Or you can try e.key






    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%2f53305534%2fbinding-data-dismiss-modal-to-a-key-click%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      The Answer:



      So I didn't need any javascript to bind a keyclick.



      Add I had to do was add tabindex='-1' to my modal id



      <div class="modal fade" role="dialog" id="PostcardModal" tabindex='-1'></div>


      This will enable the escape button to close the modal.



      Thank you to cmprogram.






      share|improve this answer



























        1














        The Answer:



        So I didn't need any javascript to bind a keyclick.



        Add I had to do was add tabindex='-1' to my modal id



        <div class="modal fade" role="dialog" id="PostcardModal" tabindex='-1'></div>


        This will enable the escape button to close the modal.



        Thank you to cmprogram.






        share|improve this answer

























          1












          1








          1







          The Answer:



          So I didn't need any javascript to bind a keyclick.



          Add I had to do was add tabindex='-1' to my modal id



          <div class="modal fade" role="dialog" id="PostcardModal" tabindex='-1'></div>


          This will enable the escape button to close the modal.



          Thank you to cmprogram.






          share|improve this answer













          The Answer:



          So I didn't need any javascript to bind a keyclick.



          Add I had to do was add tabindex='-1' to my modal id



          <div class="modal fade" role="dialog" id="PostcardModal" tabindex='-1'></div>


          This will enable the escape button to close the modal.



          Thank you to cmprogram.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 9:39









          DavaboDavabo

          60110




          60110























              0














              Have you tried e.keyCode instead of e.which? Or you can try e.key






              share|improve this answer



























                0














                Have you tried e.keyCode instead of e.which? Or you can try e.key






                share|improve this answer

























                  0












                  0








                  0







                  Have you tried e.keyCode instead of e.which? Or you can try e.key






                  share|improve this answer













                  Have you tried e.keyCode instead of e.which? Or you can try e.key







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 17:19









                  weibenfalkweibenfalk

                  55617




                  55617



























                      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%2f53305534%2fbinding-data-dismiss-modal-to-a-key-click%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 how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Syphilis

                      Darth Vader #20