The Bootstrap modal appears to be malfunctioning following the declaration of a JavaScript function

I have been working on a project where I need to display instructions to the user automatically. To achieve this, I have created a bootstrap modal which is functioning perfectly.

  <div class="modal fade" id="modal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">How to Play?</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        1. This game is created for fun, play it with your friend or enemy or whomever you want <br>
        2. Basically, this game is for my resume's sake, but feel free to play it <img src="https://awesomeopensource.com/favicon.ico" alt="" height="20px" width="20px"><br>
        3. So, there are red, green, and blue colors given in some proportion out of 255 in the format rgb(red,green,blue)
       <br> 
       4. You have to guess the color which is obtained by mixing the above proportionate colors
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
       </div>
    </div>
  </div>
</div>

I attempted to show the modal as soon as the user opens the website by writing a simple JavaScript function that displays and hides the modal using the setTimeout function.

  $(document).ready(function(){
setTimeout(function(){
$('#modal1').modal("show");
setTimeout(function(){
$('#modal1').modal("hide");
},10000);
},3000);
});

The issue arises after the modal closes automatically and when I click on the button on the footer of the modal. It does not seem to be working. You can test the functionality of the code here on the website.

Answer №1

The connection between the modal's id and the "click for the instructions" button is not properly set.

Replace exampleModalCenter with modal1 in the data-target attribute.

Therefore, your code should look like this:

<button type="button" class="btn btn-success" data-toggle="modal" data-target="#modal1">
Click For Instructions
</button>

Answer №2

Upon reviewing the code in your git project,

I noticed a minor error present.

In order for the BS modal to properly launch, two attributes are required on the button:

  1. data-target
  2. data-toggle

For data-target, you must input the ID of the modal. In this case, the provided id is incorrect. You have the option to either: change the data-target="#modal1"

or

update the modal's id to

id="exampleModalCenterTitle"

That should resolve the issue at hand.

Answer №3

Looks like the issue lies with your button

<button type="button" class="btn btn-success" data-toggle="modal" data-target="#exampleModalCenter">
    Click For Instructions
  </button>

Simply update the target attribute value to match your modal id '#modal1'

You can also utilize the provided JavaScript code snippet

$(document).ready(function(){

setTimeout(function(){
    $('#modal1').modal("show");
},3000);

$('#modal1').on('shown.bs.modal', function () {
    setTimeout(function(){
        $('#modal1').modal("hide");
    },1000);
});

});

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

Passing an event from onSubmit in React without using lambdas

Within our current project, the tslint rule jsx-no-lambda is in place. When attempting to capture event from onSubmit, this is how I typically write my code: public handleLogin = (event: React.FormEvent<HTMLFormElement>) => { event.preventDe ...

What is the method for notifying the latitude within this Google Maps javascript code?

There's a specific line that triggers an alert showing "undefined". However, when I just alert results[0].geometry.location, it displays (41.321, 41.556). My goal is to alert this value and then convert it (as an integer) to my id_lat and id_longs... ...

What is the best way to transfer the text from the input box to a div at the bottom of the page?

Recently, I came across a small piece of HTML containing two input boxes, a checkbox, and an "Add" button: <div class="row"> <div class="form-group col-xs-4"> <input type="text" class="form-control" id="items" na ...

Guide on retrieving HTML content from a URL and showing it in a div

I'm trying to retrieve the content of a URL and display it within a DIV element, but I'm struggling to achieve the desired result. Below is the code I'm using: index.js fetch('https://github.com/') .then(res => res.text()) ...

How can you determine if a polymer element has been loaded or not?

element, I am interested in dynamically importing elements using the Polymer.import( elements, callback ) method. The callback is triggered only if the elements have not been imported yet, indicating they are already loaded. My query is: Is there a conve ...

Retrieving a designated set of attributes for elements chosen using an element selector

Is there a way to retrieve specific attributes for the first 10 <p> elements in a document using jQuery? I know I can easily get the first 10 elements with $('p').slice(0,10), but I only need certain attributes like innerText for each eleme ...

Incorporating bcryptjs alongside MongoDB

I am currently developing a feature to securely encrypt user passwords using Bcrypt for my Angular application, which is integrated with MongoDB for the backend operations. Here is the implemented code snippet: Data Model var mongoose = require('mo ...

Showcasing diverse content with an Angular Dropdown Menu

I'm currently developing an angular application, and I've encountered a difficulty in displaying the user's selection from a dropdown menu. To elaborate, when a user selects a state like Texas, I want to show information such as the period, ...

Is it possible to access a service within the config function or access a provider from inside a service?

Need help configuring angular bootstrap tooltip (uibTooltip) to be disabled on mobile devices with angular-bowser for device detection. One possible solution is: isMobile = _bowser.mobile $uibTooltipProvider.options = { trigger: isMobile ? "none" : "mous ...

Automate your functions using Javascript!

Hello, I have written a code snippet that triggers an action on mouse click. Initially, I created a function that scrolls the screen to a specific element upon clicking another element: (function($) { $.fn.goTo = function() { $('html, bo ...

Retrieving the nth item from a multi-dimensional array using JavaScript

Consider this JavaScript array structure: let text = [ { line: [{ words: [{ word: [ { letter: 'H' }, { letter: 'i' }, { letter: ' ' }, ], }, { word: [ { letter: & ...

comparative analysis: nextjs getServerSideProps vs direct API calls

Trying to grasp the fundamental distinction between getServerSideProps and utilizing useSWR directly within the page component. If I implement the following code snippet in getServerSideProps: export const getServerSideProps = async () => { try { ...

Steps to fix the Uncaught TypeError error in the Wave bundle.js when integrating Wave.js into a Rails 6 application

While attempting to integrate Wave JS into my Rails 6.1.7.8 application, I encountered a specific Uncaught Type error in the Wave bundle.js: bundle.js:866 Uncaught TypeError: this._canvasElement.getContext is not a function at new t (bundle.js:866:1) Thi ...

Reacting to the dynamic removal of spaces

Is there a way to dynamically remove the space between removed chips and older chips in Material-UI when deleting one of them? <div className="row contactsContainer"> {contacts.map((contac ...

Can you explain the process of implementing @media queries in SASS?

I am having some trouble understanding the syntax for media queries in SASS. I attempted to use this particular line of code, but it resulted in an error: @media screen (max-width: 1550px) #server-name left: 80% ...

Playing sound files on Angular using Howler.JS

I am currently trying to incorporate the ability to play an mp3 file within a Cordova + Ionic hybrid app. The sound file is located at: www/sounds/dubstep/sound.mp3 I am attempting to play the file from a service placed in /www/scripts/services/global.j ...

The jQuery gallery is experiencing some functionality issues

Having an issue with the gallery on my website (currently on my computer, not yet uploaded to a server). Here is the script for the gallery (loaded from the server using PHP): $(document).ready(function() { $('.gallery').hide(); $(' ...

Tips for displaying search results on a separate page using JavaScript

I've been trying to display the result of a form on another page but I'm facing some issues. I attempted to redirect the result using window.href.location and localStorage, however, it's not working as expected. Even by utilizing click() for ...

CSS: Indication that the Text is Extending Beyond its Container

Seeking a solution to indicate overflow of text within its container, as demonstrated in this example. Essentially, a <span> with fixed dimensions. Looking for a visual cue when text exceeds boundaries. Attempted the use of text-overflow:ellipsis;, ...

Generating multiple responses from a single intention

Do you have an Alexa Skill that requires a specific initial response to an intent, followed by another response after a delay? I've tried using multiple response.tell(...) calls with a setTimeout() in between, but it only outputs the first .tell() an ...