It seems that the Bootstrap 4 Modal Pop up window is having difficulty closing

I recently completed a project on creating a matching game. After all the cards are successfully matched and the game finishes, a congratulatory modal pops up. However, I encountered an issue where the modal would not close after clicking the "Play Again" button.

Below is the index.html code snippet 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">&times;</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-->  

Additionally, here is the relevant javascript function for the 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);
    }

If you're interested in checking out my matching game project, feel free to visit the link below:

https://codepen.io/sofianayak55/pen/wEBvvJ

Answer №1

It appears that you have loaded the Bootstrap library before jQuery...

 <!--Add jQuery-->
 <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js'></script>
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
 <script src="js/app.js"></script>

To resolve this issue, try reversing the order of these two scripts. ;

The incorrect loading sequence led to an error being displayed in the 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 managed to complete the game in 15 moves within 34 seconds. ;)


EDIT
The issue was located on line #118 in the JavaScript code. Instead of using $('#myModal').toggle() (to open it once the game is over), I replaced it with $('#myModal').modal("show");. The use of data-dismiss requires the modal to be shown utilizing the modal() method rather than .toggle(). Since Bootstrap does not register the modal as being displayed with .toggle(), the dismiss functionality would not work.

You can find the updated version on this CodePen link.

I also noticed a couple resources linked locally on your server are returning a 404 error on CodePen, so please ensure to check them when copying from there.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The checkboxes seem to be malfunctioning following an ajax request

I've been attempting to utilize ajax to load data, but for some reason the checkboxes aren't functioning. Below is the HTML I'm trying to load via ajax: <div class="mt-checkbox-list" > <?php foreach($product_offerings as $row){ $c ...

How to Extract an Object from an Array using React

I have a situation where I am fetching data from an API using axios. Specifically, on my invoice details page, I am trying to retrieve data for only one invoice using the following code: const id = props.match.params.id; const invoice = useSelecto ...

JavaScript is a powerful tool for reading JSON files

I'm trying to figure out how to parse a nested object in JSON using JavaScript. Here's the code I have so far: var myRequest = new Request('test.json'); fetch(myRequest) .then(function(response) { return response.json(); }) .then( ...

Is it possible to use Vuejs v-model for a complete form instead of individual inputs?

Note: This solution is applicable for Vue 2.X I am currently working on a unique Vue.js component that has the ability to generate "custom" forms. This component essentially functions as a standalone form with multiple input fields. Users have the option ...

The response from a jQuery ajax call to an MVC action method returned empty

I am working on an inventory application with the following layout: <body> <div class="container" style="width: 100%"> <div id="header"> blahblahblah </div> <div class="row"> <div id="rendermenu ...

Issue Arising During File Transfer in Nativescript

I am currently attempting to upload an image that I captured with Nativescript to a server using a web API that I created in C# (ASP.NET). The API works perfectly fine when tested on Postman, but I encounter an error "Error During Upload" while trying to u ...

The horizontal overflow is malfunctioning, resulting in only a limited number of columns being visible on the screen

I am facing an issue with the CSS for my table. Despite using overflow-x: scroll, only half of the columns are being displayed on the screen out of the 15 total columns. Can anyone provide assistance with this problem? .table { font-family: Roboto,"He ...

Customize the serialization of a single object in Newtonsoft.Json

When comparing hashes of serialized objects on the server and client, it is important for the JSON rendering to be identical on both sides. Currently, there is an issue with a number field being serialized differently in JavaScript and .NET - causing the h ...

Unable to detect JavaScript in Chrome Developer Tools

I am experiencing a strange issue where my JavaScript code is not showing in the sources window. When I include a debugger statement in my JS and then reload the page, it successfully breaks and I can view the JavaScript code. However, the tab is labeled ...

What is the purpose of the jQuery json.text method?

I am working on creating a connect page where users can view content from various social media platforms such as Twitter, Facebook, and YouTube. Currently, I am using the getJson function in jQuery to retrieve data from the Twitter API at . However, I have ...

Assigning a property to the Next.js response object

I recently discovered a fascinating concept involving setting an attribute on the response object within a Next.js API handler and being able to access that attribute in subsequent requests. This functionality took me by surprise as I couldn't find an ...

Angular - Uploading Files

Within my $scope.accept function, I am attempting to upload some files to my server. The process works fine when using the tag in my HTML. However, I wish to prevent the page from redirecting and instead handle this with AJAX in my controller. When tryin ...

Media queries are behaving incorrectly, causing them to trigger when regular CSS should be applied, and vice versa

When I switch to laptop/desktop view, the regular CSS styles are being overridden in the inspect element, and the query styles are taking precedence. Can anyone explain why this is happening? For instance, let's take a look at the code for the nav se ...

What steps should be taken to resolve the update problem in Sequelize?

Having an issue with the update method in MySQL ORM. User.update({ ResetPasswordToken : resetPasswordToken },{ where: { UserName: 'testuser' } }) Receive this Sequelize Log: Running query: UPDATE Users SET ResetPasswordToken=?,updat ...

What are some ways to customize text using Font-Awesome?

<i class="fa fa-envelope fa-lg"> <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aad8c5c8ccd8cbc4eacdc7cbc3c84ac9fbccdbdb">[email protected]</a> </i> I need assistance with changing the font type f ...

Disable link 2 when link 1 is clicked

Looking to create a feedback form with two exclusive links. Want to ensure that if someone clicks the first link, they cannot click the second link and vice versa. Interested in exploring options like using cookies to prevent multiple clicks or possibly ...

Styled-Component: Incorporating Variables into Styled-Component is my goal

Currently, I am working on an app and have created a separate file for styling. I decided to use style-components for custom CSS, but faced an issue where I couldn't access variables instead of HEX values. Even after storing color values in a variable ...

Events that are loaded using jQuery

My webpage features dynamic content generated by PHP, and I am looking to utilize jQuery's load function to refresh the div container periodically. This will ensure that new content is displayed on top of the existing content. The jQuery function I a ...

Developing an intricate nesting structure for an object

this is my code snippet: "book" => {b:{o:{o:k:{'end':true} could someone please provide an explanation or a helpful link for this? const ENDS_HERE = '__ENDS_HERE' class Trie { constructor() { this.node = {}; } insert(w ...

Having trouble sending information to MailChimp

I am currently facing an issue with connecting my node.js application to my Mailchimp account. Despite submitting the data, it does not appear in my Mailchimp account. const express = require("express"); const bodyParser = require("body-pa ...