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
3 Answers
Reset to default 3Modify 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条)