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

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현