Is it possible to implement a Bootstrap 4 modal without using jQuery when the page loads

I am struggling to get a modal to show up on page load using Bootstrap 4. The code for the modal is sourced from online resources:

</body>

<div class="modal fade" id="cookieModal" style="display: block;>
 <div class="modal-dialog" role="document">
  <div class="modal-content">
    <div class="modal-header">
      <h4 class="modal-title">Modal title</h4>
      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true"&rt;&times;</span>
      </button>
    </div>
    <div class="modal-body">
      <p>Modal body content...........</p>
    </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><!-- /.modal-content --&rt;
 </div><!-- /.modal-dialog --&rt;
</div><!-- /.modal --&rt;

<script src="/public/js/logic.js"></script>

</body>

Even though I tried giving an "id" name to the modal and setting "style="display: block"", the modal does not appear to display. In my external ("logic") file, I attempted the following pure JavaScript approach:

addEventListener("load", initialize); 


function initialize() {

var _warning = document.getElementById("cookieModal");  //for cookie modal display on page load...
_warning.style.display = "block";  //show the cookie modal...

}

If anyone can offer advice without relying on JQuery, I would greatly appreciate it as I prefer to use pure JS over JQuery. Thank you in advance.

Answer №1

To effectively utilize Bootstrap 4 modals, incorporating jQuery is essential. Simply altering the display property will not suffice.

For those who prefer to avoid jQuery dependency, consider leveraging the most recent iteration of Bootstrap (5.0.0), as it no longer relies on jQuery for any functionality.

Answer №2

If your question is directed towards me, this method will work well: Utilizing jQuery (apologies for using jQuery, but that's how Bootstrap suggests doing it).

 $(document).ready(function() {
    $('#cookieModal').modal('show');
//To automatically hide it after 5 seconds.
    setTimeout(function() {
      $("#cookieModal").modal('hide');
    }, 5000);
  });

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

Angular's unconventional solution to Firebase

I've been having trouble integrating Firebase with Angular. When I encountered an error stating that firebase.initializeApp is not a function, I hit a roadblock. const firebase = require("firebase"); (Previously, it was written as: import * as fireb ...

Eliminate items from the array if the property of an object includes any of the provided substrings

In an attempt to minimize the objects within arrays that have a property "Title" containing any substring listed in a separate JSON file, I have been struggling with filtering the arrays based on the object's property. However, I believe using the red ...

Why do object == this, but object.member != this.member in the realm of Javascript?

I currently have an object labeled o along with a reference to it. Inside the scope of object o, I define a member named m. Within object o, I execute: o.m = "blah" When I try to access m outside of object o: console.log(o.m) The output is not "blah" ...

JSONP OpenWeather API

I've been trying to access and retrieve weather data from OpenWeather using their API, but unfortunately, I'm facing some difficulties in getting it to work. It seems like there might be an issue with the way I am fetching the JSON data. To quer ...

What is the correct method for calculating the sum of one input field and multiple output fields?

I have a single input field and multiple calculated output fields using JavaScript :Click here to view the screenshot of the calculated problem You can see the live preview on this link: . When you visit this link, enter "Base on your annual bill amount: ...

Obtain the name of the client's computer within a web-based application

I am working on a Java web application that requires the computer name of clients connecting to it. Initially, I attempted to retrieve this information using JavaScript and store it in a hidden form field. However, I discovered that JavaScript does not hav ...

Retrieve the Vue.js JavaScript file from the designated static directory

This is my inaugural attempt at creating a web application, so I am venturing into Vue.js Javascript programming as a newcomer. I have chosen to work with the Beagle Bootstrap template. Within my Static folder, I have a file named app-charts-morris.js whi ...

Automatically unselect the "initially selected item" once two items have been selected in Material UI

As someone new to web development, I'm struggling with a specific task. Here is the issue at hand: I have three checkboxes. If box1 and then box2 are selected, they should be marked. However, if box3 is then selected, box1 should automatically unchec ...

Align three out of seven items in the navbar to the right using Bootstrap 4.1

Bootstrap 4.1 offers a great way to create a Navbar with multiple menu items, but I'm struggling to right justify three specific menu items within the Navbar. I've tried different methods, including using align-self-end, justify-content-end, and ...

Is HTML unable to display decoded %3C?

Two blocks of code with different input strings yield different results: // This does not change HTML var str = "%3Cdiv%3E"; // <div> var str_dec = decodeURIComponent(str); console.log(str_dec); // Console output is `<div>` document.getEleme ...

My React app experienced a severe crash when trying to render an animation in L

Currently, I am working on a React application that was set up using Vite. I recently incorporated an animation using Lottie, and although I was successful in implementing it, I encountered a problem when navigating between different pages of my applicati ...

Uncovering the status code from a WebSocket upgrade request using the socket.io client

I have updated the upgrade event listener in my node.js code as follows: server.on('upgrade', (req, socket, head) => { socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n'); socket.destroy(); return; } Is ther ...

Error: The operation cannot be performed as the specified function is invalid

Currently working on some jasmine testing in JS. I've created a testing function that looks good so far (only running the first test at the moment). describe('Anagram', function() { it('no matches',function() { var subject ...

What is the best way to iterate through an array and dynamically output the values?

I am facing an issue with a function that retrieves values from an array, and I wish to dynamically return these values. const AvailableUserRoles = [ { label: 'Administrator', value: 1 }, { label: 'Count', value: ...

Create a program that continues to run even if the user decides to close the window

Currently, I am in the process of developing a React project. There is a requirement for a function to continuously retrieve data using a web socket every second. However, I am faced with the challenge of ensuring that this function with the web socket s ...

showcasing a map on an HTML webpage

Seeking advice on integrating a map feature in HTML to mark store locations. Working on an app for a business community and need help with this specific functionality. Your assistance would be greatly appreciated! ...

Creating a HTML drop-down menu that requires the user to make a selection in the first drop-down before moving on to the next drop

Is there a way to prevent the user from selecting the second drop-down menu (time) until they have chosen an option from the first drop-down menu (movie)?<html> <body> <h1>Using the select element in HTML</h1> <p>The selec ...

Interactive calendar feature displaying events upon hovering over a date

I need some assistance with displaying a drop-down list on full calendar events when hovering over the events. Can someone provide guidance? Here is a glimpse of what I currently have: https://i.sstatic.net/fZXMH.png I've attempted setting the z-in ...

A common error occurs when trying to utilize the jQuery $.post() method in a file where "output" is not defined

I've recently updated my login scripts and sessions, and now I'm facing an issue with a dynamic search feature on my website. The search functionality is powered by jQuery and a PHP search file that retrieves results from a database of members. D ...

Sending data from an external input field to a form using AngularJS programmatically with element name

I am facing a challenge where I need to include an Input element in a Form, even though the Input is located outside of the form. While I have managed to add the input using form.$addControl(outerInput), it is not producing the desired outcome as shown in ...