javascript - How to open bootstrap modal using a button click - Stack Overflow

I have developed a simple js pig game. You can see it here: At first, it will open a Bootstrap Modal bo

I have developed a simple js pig game. You can see it here:

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: /

Thanks.

I have developed a simple js pig game. You can see it here: http://creativeartbd./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/r8cga7L5/

Thanks.

Share Improve this question asked Mar 26, 2019 at 5:54 ShibbirShibbir 2,0414 gold badges30 silver badges67 bronze badges 8
  • $("#myModal").modal('show'); – Pranav C Balan Commented Mar 26, 2019 at 5:55
  • you can use $("#myModal").modal('open'); $("#myModal").modal('show'); $("#myModal").modal('toggle'); – Devsi Odedra Commented Mar 26, 2019 at 5:56
  • then its showing this error: bootstrap.min.js:6 Uncaught TypeError: f[b] is not a function – Shibbir Commented Mar 26, 2019 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 Commented Mar 26, 2019 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/z12uL3ba – Sethuraman Commented Mar 26, 2019 at 6:32
 |  Show 3 more ments

3 Answers 3

Reset to default 3

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>

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>

You can just call it like this:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745261414a4619228.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信