How to open bootstrap modal using a button clickHow do JavaScript closures work?How to horizontally center a <div>?How do I detect a click outside an element?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I redirect to another webpage?How can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Report how much space is used and available in storage in ZFS on FreeBSD

To accent or not to accent in Greek

Find values of x so that the matrix is invertible

Can I call 112 to check a police officer's identity in the Czech Republic?

Why is dry soil hydrophobic? Bad gardener paradox

Won 50K! Now what should I do with it

How can an advanced civilization forget how to manufacture its technology?

Data standardization vs. normalization for clustering analysis

How does one stock fund's charge of 1% more in operating expenses than another fund lower expected returns by 10%?

If the derivative of a function is square of it then it is constant

Is `curl something | sudo bash -` a reasonably safe installation method?

Are lithium batteries allowed in the International Space Station?

Why do they not say "The Baby"

Why the term 'unified' in "unified mass unit"?

What is the English equivalent of 干物女 (dried fish woman)?

Is it rude to tell recruiters I would only change jobs for a better salary?

Hacker Rank : Electronics Shop

What does "Fotze" really mean?

In which ways do anagamis still experience ignorance?

Modeling, view and projection transformation using vector and point in homogenous form

Should you avoid redundant information after dialogue?

Can a continent naturally split into two distant parts within a week?

Bob's unnecessary trip to the shops

Measuring mystery distances



How to open bootstrap modal using a button click


How do JavaScript closures work?How to horizontally center a <div>?How do I detect a click outside an element?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I redirect to another webpage?How can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have developed a simple js pig game. You can see it here: http://creativeartbd.com/demo/game/js-pig-game



At first, it will open a Bootstrap Modal box to set a winning score. Now, the game will begin by click on the Roll Dice button.



Well, now if you WIN then you can press the New Game button to re-play the game. Here, I want to open the bootstrap modal again when I click on the New Game button. For that I have write this code:



document.querySelector(".btn-new-game").addEventListener("click", function () 
init();
document.getElementById("myModal").modal('show');
);


but it's showing me an error message on console log:



Uncaught TypeError: document.getElementById(...).modal is not a function


Maybe this is because of Javascript IIFE. I don't know exactly :(



I have also tried with this code:



document.getElementById("myModal").click(); 


but no luck :(



My HTML and js code is a bit long that's why I don't put here. You can find it here: https://jsfiddle.net/r8cga7L5/



Thanks.










share|improve this question






















  • $("#myModal").modal('show');

    – Pranav C Balan
    Mar 26 at 5:55











  • you can use $("#myModal").modal('open'); $("#myModal").modal('show'); $("#myModal").modal('toggle');

    – Devsi Odedra
    Mar 26 at 5:56











  • then its showing this error: bootstrap.min.js:6 Uncaught TypeError: f[b] is not a function

    – creativeartbd
    Mar 26 at 5:58











  • You already using model on window load using Jquery the same way you can open model on button click also. document.querySelector(".btn-new-game").addEventListener("click", function () init(); $('#myModal').modal('show'); );

    – Sethuraman
    Mar 26 at 6:07







  • 1





    please check this fiddle with what i have added the same code of yours like $('#myModal').modal('show'); and its working -- jsfiddle.net/z12uL3ba

    – Sethuraman
    Mar 26 at 6:32


















0















I have developed a simple js pig game. You can see it here: http://creativeartbd.com/demo/game/js-pig-game



At first, it will open a Bootstrap Modal box to set a winning score. Now, the game will begin by click on the Roll Dice button.



Well, now if you WIN then you can press the New Game button to re-play the game. Here, I want to open the bootstrap modal again when I click on the New Game button. For that I have write this code:



document.querySelector(".btn-new-game").addEventListener("click", function () 
init();
document.getElementById("myModal").modal('show');
);


but it's showing me an error message on console log:



Uncaught TypeError: document.getElementById(...).modal is not a function


Maybe this is because of Javascript IIFE. I don't know exactly :(



I have also tried with this code:



document.getElementById("myModal").click(); 


but no luck :(



My HTML and js code is a bit long that's why I don't put here. You can find it here: https://jsfiddle.net/r8cga7L5/



Thanks.










share|improve this question






















  • $("#myModal").modal('show');

    – Pranav C Balan
    Mar 26 at 5:55











  • you can use $("#myModal").modal('open'); $("#myModal").modal('show'); $("#myModal").modal('toggle');

    – Devsi Odedra
    Mar 26 at 5:56











  • then its showing this error: bootstrap.min.js:6 Uncaught TypeError: f[b] is not a function

    – creativeartbd
    Mar 26 at 5:58











  • You already using model on window load using Jquery the same way you can open model on button click also. document.querySelector(".btn-new-game").addEventListener("click", function () init(); $('#myModal').modal('show'); );

    – Sethuraman
    Mar 26 at 6:07







  • 1





    please check this fiddle with what i have added the same code of yours like $('#myModal').modal('show'); and its working -- jsfiddle.net/z12uL3ba

    – Sethuraman
    Mar 26 at 6:32














0












0








0








I have developed a simple js pig game. You can see it here: http://creativeartbd.com/demo/game/js-pig-game



At first, it will open a Bootstrap Modal box to set a winning score. Now, the game will begin by click on the Roll Dice button.



Well, now if you WIN then you can press the New Game button to re-play the game. Here, I want to open the bootstrap modal again when I click on the New Game button. For that I have write this code:



document.querySelector(".btn-new-game").addEventListener("click", function () 
init();
document.getElementById("myModal").modal('show');
);


but it's showing me an error message on console log:



Uncaught TypeError: document.getElementById(...).modal is not a function


Maybe this is because of Javascript IIFE. I don't know exactly :(



I have also tried with this code:



document.getElementById("myModal").click(); 


but no luck :(



My HTML and js code is a bit long that's why I don't put here. You can find it here: https://jsfiddle.net/r8cga7L5/



Thanks.










share|improve this question














I have developed a simple js pig game. You can see it here: http://creativeartbd.com/demo/game/js-pig-game



At first, it will open a Bootstrap Modal box to set a winning score. Now, the game will begin by click on the Roll Dice button.



Well, now if you WIN then you can press the New Game button to re-play the game. Here, I want to open the bootstrap modal again when I click on the New Game button. For that I have write this code:



document.querySelector(".btn-new-game").addEventListener("click", function () 
init();
document.getElementById("myModal").modal('show');
);


but it's showing me an error message on console log:



Uncaught TypeError: document.getElementById(...).modal is not a function


Maybe this is because of Javascript IIFE. I don't know exactly :(



I have also tried with this code:



document.getElementById("myModal").click(); 


but no luck :(



My HTML and js code is a bit long that's why I don't put here. You can find it here: https://jsfiddle.net/r8cga7L5/



Thanks.







javascript html






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 5:54









creativeartbdcreativeartbd

5724 silver badges14 bronze badges




5724 silver badges14 bronze badges












  • $("#myModal").modal('show');

    – Pranav C Balan
    Mar 26 at 5:55











  • you can use $("#myModal").modal('open'); $("#myModal").modal('show'); $("#myModal").modal('toggle');

    – Devsi Odedra
    Mar 26 at 5:56











  • then its showing this error: bootstrap.min.js:6 Uncaught TypeError: f[b] is not a function

    – creativeartbd
    Mar 26 at 5:58











  • You already using model on window load using Jquery the same way you can open model on button click also. document.querySelector(".btn-new-game").addEventListener("click", function () init(); $('#myModal').modal('show'); );

    – Sethuraman
    Mar 26 at 6:07







  • 1





    please check this fiddle with what i have added the same code of yours like $('#myModal').modal('show'); and its working -- jsfiddle.net/z12uL3ba

    – Sethuraman
    Mar 26 at 6:32


















  • $("#myModal").modal('show');

    – Pranav C Balan
    Mar 26 at 5:55











  • you can use $("#myModal").modal('open'); $("#myModal").modal('show'); $("#myModal").modal('toggle');

    – Devsi Odedra
    Mar 26 at 5:56











  • then its showing this error: bootstrap.min.js:6 Uncaught TypeError: f[b] is not a function

    – creativeartbd
    Mar 26 at 5:58











  • You already using model on window load using Jquery the same way you can open model on button click also. document.querySelector(".btn-new-game").addEventListener("click", function () init(); $('#myModal').modal('show'); );

    – Sethuraman
    Mar 26 at 6:07







  • 1





    please check this fiddle with what i have added the same code of yours like $('#myModal').modal('show'); and its working -- jsfiddle.net/z12uL3ba

    – Sethuraman
    Mar 26 at 6:32

















$("#myModal").modal('show');

– Pranav C Balan
Mar 26 at 5:55





$("#myModal").modal('show');

– Pranav C Balan
Mar 26 at 5:55













you can use $("#myModal").modal('open'); $("#myModal").modal('show'); $("#myModal").modal('toggle');

– Devsi Odedra
Mar 26 at 5:56





you can use $("#myModal").modal('open'); $("#myModal").modal('show'); $("#myModal").modal('toggle');

– Devsi Odedra
Mar 26 at 5:56













then its showing this error: bootstrap.min.js:6 Uncaught TypeError: f[b] is not a function

– creativeartbd
Mar 26 at 5:58





then its showing this error: bootstrap.min.js:6 Uncaught TypeError: f[b] is not a function

– creativeartbd
Mar 26 at 5:58













You already using model on window load using Jquery the same way you can open model on button click also. document.querySelector(".btn-new-game").addEventListener("click", function () init(); $('#myModal').modal('show'); );

– Sethuraman
Mar 26 at 6:07






You already using model on window load using Jquery the same way you can open model on button click also. document.querySelector(".btn-new-game").addEventListener("click", function () init(); $('#myModal').modal('show'); );

– Sethuraman
Mar 26 at 6:07





1




1





please check this fiddle with what i have added the same code of yours like $('#myModal').modal('show'); and its working -- jsfiddle.net/z12uL3ba

– Sethuraman
Mar 26 at 6:32






please check this fiddle with what i have added the same code of yours like $('#myModal').modal('show'); and its working -- jsfiddle.net/z12uL3ba

– Sethuraman
Mar 26 at 6:32













3 Answers
3






active

oldest

votes


















0














Any specific reason you want the modal to open from JS?



Try this for the Bootstrap 4 approach:



<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>

<!-- Modal -->
<div class="modal" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>





share|improve this answer






























    0














    Modify the button like this,
    It's working perfectly fine



    <button data-toggle="modal" data-target="#myModal" class="btn btn-new-game">New Game(+)</button>





    share|improve this answer






























      -1














      You can just call it like this:



      document.getElementById("myModal").modal();






      share|improve this answer























      • Then it's showing me: TypeError: document.getElementById(...).modal is not a function

        – creativeartbd
        Mar 26 at 5:59











      • can you check my js code plz?

        – creativeartbd
        Mar 26 at 5:59













      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%2f55350639%2fhow-to-open-bootstrap-modal-using-a-button-click%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Any specific reason you want the modal to open from JS?



      Try this for the Bootstrap 4 approach:



      <!-- Button trigger modal -->
      <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
      Launch demo modal
      </button>

      <!-- Modal -->
      <div class="modal" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
      <div class="modal-content">
      <div class="modal-header">
      <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">×</span>
      </button>
      </div>
      <div class="modal-body">
      ...
      </div>
      <div class="modal-footer">
      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      <button type="button" class="btn btn-primary">Save changes</button>
      </div>
      </div>
      </div>
      </div>





      share|improve this answer



























        0














        Any specific reason you want the modal to open from JS?



        Try this for the Bootstrap 4 approach:



        <!-- Button trigger modal -->
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
        Launch demo modal
        </button>

        <!-- Modal -->
        <div class="modal" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
        <div class="modal-content">
        <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">×</span>
        </button>
        </div>
        <div class="modal-body">
        ...
        </div>
        <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
        </div>
        </div>
        </div>
        </div>





        share|improve this answer

























          0












          0








          0







          Any specific reason you want the modal to open from JS?



          Try this for the Bootstrap 4 approach:



          <!-- Button trigger modal -->
          <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
          Launch demo modal
          </button>

          <!-- Modal -->
          <div class="modal" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
          <div class="modal-content">
          <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
          </button>
          </div>
          <div class="modal-body">
          ...
          </div>
          <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
          </div>
          </div>
          </div>
          </div>





          share|improve this answer













          Any specific reason you want the modal to open from JS?



          Try this for the Bootstrap 4 approach:



          <!-- Button trigger modal -->
          <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
          Launch demo modal
          </button>

          <!-- Modal -->
          <div class="modal" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
          <div class="modal-content">
          <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
          </button>
          </div>
          <div class="modal-body">
          ...
          </div>
          <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
          </div>
          </div>
          </div>
          </div>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 26 at 7:09









          Varun JoshiVarun Joshi

          10310 bronze badges




          10310 bronze badges























              0














              Modify the button like this,
              It's working perfectly fine



              <button data-toggle="modal" data-target="#myModal" class="btn btn-new-game">New Game(+)</button>





              share|improve this answer



























                0














                Modify the button like this,
                It's working perfectly fine



                <button data-toggle="modal" data-target="#myModal" class="btn btn-new-game">New Game(+)</button>





                share|improve this answer

























                  0












                  0








                  0







                  Modify the button like this,
                  It's working perfectly fine



                  <button data-toggle="modal" data-target="#myModal" class="btn btn-new-game">New Game(+)</button>





                  share|improve this answer













                  Modify the button like this,
                  It's working perfectly fine



                  <button data-toggle="modal" data-target="#myModal" class="btn btn-new-game">New Game(+)</button>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 26 at 8:32









                  KarthikKarthik

                  11 silver badge2 bronze badges




                  11 silver badge2 bronze badges





















                      -1














                      You can just call it like this:



                      document.getElementById("myModal").modal();






                      share|improve this answer























                      • Then it's showing me: TypeError: document.getElementById(...).modal is not a function

                        – creativeartbd
                        Mar 26 at 5:59











                      • can you check my js code plz?

                        – creativeartbd
                        Mar 26 at 5:59















                      -1














                      You can just call it like this:



                      document.getElementById("myModal").modal();






                      share|improve this answer























                      • Then it's showing me: TypeError: document.getElementById(...).modal is not a function

                        – creativeartbd
                        Mar 26 at 5:59











                      • can you check my js code plz?

                        – creativeartbd
                        Mar 26 at 5:59













                      -1












                      -1








                      -1







                      You can just call it like this:



                      document.getElementById("myModal").modal();






                      share|improve this answer













                      You can just call it like this:



                      document.getElementById("myModal").modal();







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 26 at 5:58









                      Jan LoisJan Lois

                      1899 bronze badges




                      1899 bronze badges












                      • Then it's showing me: TypeError: document.getElementById(...).modal is not a function

                        – creativeartbd
                        Mar 26 at 5:59











                      • can you check my js code plz?

                        – creativeartbd
                        Mar 26 at 5:59

















                      • Then it's showing me: TypeError: document.getElementById(...).modal is not a function

                        – creativeartbd
                        Mar 26 at 5:59











                      • can you check my js code plz?

                        – creativeartbd
                        Mar 26 at 5:59
















                      Then it's showing me: TypeError: document.getElementById(...).modal is not a function

                      – creativeartbd
                      Mar 26 at 5:59





                      Then it's showing me: TypeError: document.getElementById(...).modal is not a function

                      – creativeartbd
                      Mar 26 at 5:59













                      can you check my js code plz?

                      – creativeartbd
                      Mar 26 at 5:59





                      can you check my js code plz?

                      – creativeartbd
                      Mar 26 at 5:59

















                      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%2f55350639%2fhow-to-open-bootstrap-modal-using-a-button-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

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript