Reputation: 11
I did a project on matching game. Where after all cards match and game finish, a congratulation modal pop up. But I could not able to know why it does not close, after clicking the play again button.
Here is the index.html for the modal window
<!--Add Bootstrap Modal Alert Window-->
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal-label" aria-hidden="true">
<div class="modal-dialog">
<!--Modal Content-->
<div class="modal-content">
<!--Modal Header-->
<div class="modal-header">
<h4 class="modal-title" id="myModal-label">Congratulations!!!</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<!--Modal Body-->
<div class="modal-body">
<p id="myText"></p>
</div>
<!--Modal Footer-->
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn btn-success btn-default" onclick="gameStart(), $rating.removeClass('fa-star-o').addClass('fa-star');">Play Again!</button>
</div> <!--modal footer-->
</div> <!--modal-content-->
</div> <!--modal-dialog-->
</div> <!--modal-->
And the javascript for Modal:
function gameOver(moves, score) {
$('#myText').text(`Time: ${second} Seconds, Your Move: ${moves} Moves, Total
Score: ${score}, Well done!!!`);
$('#myModal').toggle();
}
if (totalCard === match) {
rating(moves);
let score = rating(moves).score;
setTimeout(function () {
gameOver(moves, score);
},800);
}
Here is the link for my matching game project:
https://codepen.io/sofianayak55/pen/wEBvvJ
Upvotes: 0
Views: 1393
Reputation: 33933
You are loading the Bootstrap library before jQuery...
<!--Add jQuery-->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js'></script>
<script src="js/app.js"></script>
Reverse the order of those 2. ;)
This created the below error in console:
Uncaught TypeError: Cannot read property 'fn' of undefined
at util.js:68
at util.js:10
at bootstrap.min.js:6
at bootstrap.min.js:6
I had the game done in 15 moves, 34 seconds. ;)
$('#myModal').toggle()
(at game over, to open it).
I replaced id with $('#myModal').modal("show");
. data-dismiss
needs the modal to be shown using the modal()
method. .toggle()
is the jQuery method to display/hide an element. So since Bootstrap doesn't know the modal is shown, the dismiss
doesn't work.
Here is your CodePen updated.
I commented a couple ressources that are local on your server... but are 404 in CodePen... So be aware if you copy/paste from it.
Upvotes: 2