Is there a way to prevent a Bootstrap alert from disappearing on its own?

I'm having trouble with my button's alert feature. When clicked, the alert should display and stay visible until it's closed.

However, on my website, the alert quickly flashes on the screen every time the button is pressed.

On my jsfiddle demo, the alert stays visible after being triggered but does not reappear when the button is clicked again.

Check out the code here!

Any suggestions or solutions would be greatly appreciated.

html

<div class="form-group">
  <div class="col-lg-10 col-lg-offset-2">
    <button type="submit" class="btn btn-primary">Submit</button>
    <div class="alert alert-dismissible alert-success">
      <button type="button" class="close" data-dismiss="alert">&times;</button>
      <strong>Completed!</strong> Form successfully submitted...
    </div>
  </div>
</div>

css

<style>
    .alert {
        display: none;
    }
</style>

js

<script>
    $(document).ready(function () {
        $('button').click(function () {
            $('.alert').show()
        })
    });
</script>

Answer №1

Give this a shot: Check out the DEMO

If you use data-dismiss="alert", it will completely remove the alert div. Try the following approach.

<body>
    <button type="submit" class="btn btn-primary">Submit</button>
    <div class="alert alert-dismissible alert-success">
        <button type="button" class="close">&times;</button>
        <strong>Completed!</strong> Form successfully submitted...
    </div>
</body>

JS:

$(document).ready(function(){
    $('button').click(function(){
        $('.alert').show()
    }); 

    $('button').click(function(){
        $(this).parents('div').hide();
    }); 
});

CSS:

.alert{
    display: none;
}

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

Is there a way to stop text from wrapping between words and punctuation marks using CSS or jQuery?

There is a paragraph containing some text. One issue I am facing is that punctuation at the end of a word may get wrapped to the next line, as shown here: This is the sentence, This is a new line Is there a way to fix this using CSS or jQuery? ...

The serialize() function in jQuery is not functioning as expected

Greetings to all! I attempted to serialize a form using the jQuery method serialize() by its unique ID. Here is the HTML code snippet: <form name="structure_form" id="structure_form" method="post"> <div id="add_sections" class="postbox add_s ...

Developing an array-based multiple choice quiz

Being a complete novice, I am currently immersed in designing a multiple-choice quiz using arrays. The questions all follow a similar template: "Which word in the following has a similar meaning to '_________'." To implement this, I have created ...

When the AJAX function is called again, the previous loading process is interrupted, without the use of jQuery

I have created a function that loads a URL into an element, but it seems to be encountering issues when called repeatedly in a loop to load multiple elements. The current load operation stops prematurely: function loadDataFromURL(url, elementId) { var ...

Trouble with transferring data from a div to an input field using jQuery

Currently, I am implementing an inplace edit feature for a form. The setup consists of two divs, where one holds display elements and the other contains the input form. Upon clicking the edit button, the data shifts from the display div to the input form. ...

Tips on configuring the path to incorporate Bootstrap CSS, JavaScript, and font files/classes into HTML when the HTML file and Bootstrap files are located in different directories

<!DOCTYPE html> <html> <head> <title>Unique Demo</title> <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" media="screen"> </head> <body> <div class="container"> <form ...

IE11 React application cannot recognize the <main> tag

When using my React application, I encountered the following error message in the dev console while using IE11: Warning: The tag <main> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase let ...

Issues with data communication in AJAX and Node JS/Express post requests

I'm currently exploring Node.js and I'm running into an issue with app.post. Everything seems to be working fine, as I can see the console log whenever the action is executed. However, the data sent by AJAX from main.js does not seem to be receiv ...

What are some effective ways to utilize a marquee?

I am looking for a way to duplicate the <a> tag within a DIV named box so that the text is displayed on a continuous line marquee without any breaks. As I am new here, please feel free to ask for more information in the comments. Thank you. $(&apo ...

What is the best way to ensure grid element takes up 100% of the available height within a flex column

I am working with this HTML structure: https://i.stack.imgur.com/fy5Yz.png My goal is to have the .SceneWithHistory-Container take up 100% of the available height in its parent element. Below is my CSS code: .Module-Container margin-top: 120px; displ ...

Can IE(7?) cause distortion of backgrounds from sprites?

This task is giving me a headache. We're almost finished with revamping our website. The last step involves consolidating all the glyphs and icons into a sprite. These images are transparent .png files, so we made sure the sprite maintains transparen ...

Tips on delivering value each time the increase button is clicked on a dynamically generated input using jQuery

I am currently utilizing jQuery dynamic table inputs within my form. The issue I am facing is that when I select a country from the dropdown, the value is captured correctly. However, if I add another input field dynamically and then select a different cou ...

Tips for concealing a dynamic DOM element using JQuery after it has been generated

My current project involves creating a form that allows users to dynamically add text fields by clicking on an "add option" button. Additionally, they should be able to remove added fields with a "remove option" link that is generated by JQuery along with ...

Unexpected behavior: jQuery AJAX parameters not properly received by PHP script

Here is some javaScript code that I have: const getReport = { jobID: $('#jobID').val(), startDate: $('#summaryStart').val(), endDate: $('#summaryEnd').val(), tableType: 'Summary', }; $.ajax({ url ...

Error: JSONP unexpectedly encountered token '<' causing a SyntaxError

Issue: Error: Uncaught SyntaxError: Unexpected token < My Attempt Method Used: Jsonp Result: received XML Response 200. Due to Cross Domain Request, data type jsonp was utilized Code snippet: $.ajax({ url: "some url", headers: { ...

Learn how to efficiently import scripts and styles from WordPress plugins with the help of the wp_enqueue_script function

After creating my own custom Wordpress theme, everything seemed to be running smoothly. However, I soon encountered an issue where certain plugins were not functioning properly with the theme. It seems that the theme is unable to import the necessary style ...

Are you tuned in to the Bootstrap collapse events switcheroo?

I created a customized Bootstrap 5 accordion with the following structure: <div class="accordion" id="my_accordion"> <div class="accordion-item"> <h2 class="accordion-header" id="heading_bar ...

A guide on dynamically generating Bootstrap rows and columns using an ngFor loop

Receiving data from the backend with a number of maps, I need to display them on the page. The placement of the maps is based on even and odd conditions. (For example, if there is only one map, it should occupy the entire page. If there are two maps, they ...

Spin the SVG image upon clicking the Bootstrap 4 collapse feature

I am attempting to create a toggle effect for an SVG icon, rotating it from the up position to the down position when clicked. While a pure CSS solution would be ideal, I am struggling to implement it for an SVG icon. Unfortunately, I cannot use a font ic ...

Having trouble with Ajax not sending data to PHP in JSON format

After attempting various methods using different ajax techniques, I still cannot get this to work. Being new to ajax and json, I initially thought that the issue might be due to the data not being in a proper form. However, other ajax processes structured ...