Alert Div from Bootstrap fails to appear during the second ajax request if the first ajax request is canceled

I've implemented a dismissible alert in my HTML file that starts off hidden. Here's the code snippet:

<div class="alert alert-dismissible" role="alert" id="msgdiv" style="margin-bottom:5px;display: none;">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <span id="emsg"></span>
</div>

Whenever I make an Ajax request to submit the form, I reveal this previously hidden div based on the response. Subsequently, I add either a success or error class to this div.

console.log($("#msgdiv"));
$("#msgdiv").show();
if (result.is_success) {
    $("#msgdiv").removeClass("alert-warning");
    $("#msgdiv").addClass("rlt-success");

} else {
    $("#msgdiv").removeClass("alert-success");
    $("#msgdiv").addClass("rlt-warning");
}
$("#emsg").html(result.message);

Upon receiving the response for the first time, the div is displayed. By clicking the cross button, the div can be dismissed. However, when I submit the form with new values using Ajax for the second time without refreshing the page, the div is not displayed. Strangely, the same result is returned on both occasions. I checked the console log output of the div element in both scenarios and here are the findings:

First Time:

[div#msgdiv.alert.alert-dismissible, context: document, selector: "#msgdiv"]

Second Time:

n.fn.init {context: document, selector: "#msgdiv"}

For further details, refer to the image below.

https://i.sstatic.net/k5dEX.png

Why is this discrepancy happening? What does the difference in console logs signify from both HTML and JS perspectives?

Answer №1

Once an alert has been closed, it will no longer be visible in the webpage.

If you wish for the alert to reappear at a later time, you can adjust the close button by removing the data-dismiss="alert" attribute like so:

<div class="alert alert-dismissible" role="alert" id="msgdiv" style="margin-bottom:5px;display: none;">
    <button type="button" class="close" aria-label="Close"><span aria-hidden="true>&times;</span></button>
    <spanid="emsg"></span>
</div>

After making this adjustment, you can bind the close button to simply hide the alert when clicked:

$('.alert .close').click(function() {
   $(this).parent().hide();
});

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

Error in Angular 7: Trying to assign a 'void' type to a 'ObservableInput<{}>' type

I can't seem to understand why I am encountering this error fetchFromURL(url: string): any { return this.apiService.get(url).pipe(map(data => data)); } fetchNextPage(): Observable<any> { const url = 'url'; this.f ...

Utilizing Select2 within the VueJS 3 composition API

How do I implement a select2 component in VueJS3 composition? I am currently learning VueJS 3 and encountering an error when trying to create a select2 component. I previously used this code on VueJS 2 and it was working fine. Can someone help me fix the ...

Firebase version 9 - Retrieve the content of a referenced document

I'm having trouble locating the documentation I need. Within my Firebase Firestore project, there is a collection called users. Within users, there are three collections: companies, policies, and stores. In the policies collection, I have fields for ...

The symbol in my Google Maps path is off-center and not aligned correctly

Hey everyone, I'm currently working on a project that involves a plane moving along a polyline, but I'm facing an issue where the path symbol is not centered as demonstrated in this image This is what I have: Here, I define the path symbol as t ...

JavaScript Processing Multiple Input Values to Produce an Output

Hello, I am working on creating a stamp script that will allow users to enter their name and address into three fields. Later, these fields will be displayed in the stamp edition. Currently, I have set up 3 input fields where users can input their data. He ...

When a nested CSS Grid is inserted into an HTML table element, it causes horizontal overflow

     Generating an invoice dynamically with a CSS grid poses some challenges. To maintain a specific format, I need to define the number of rows and columns in advance along with column widths. Using template rows and columns for this purpose restrict ...

Adding events to a TinyMCE iframe in WordPress

Recently, I implemented some customized div wrapper styles in Wordpress. However, when utilizing these styles on a page, I encountered difficulties adding content below them. Despite using CSS:after within the divs to generate a selectable area, this metho ...

The appearance of the check box remains stagnant visually

Having trouble with dynamically changing the state of a checkbox based on a database value. Even though the value changes after a button click, the visual state of the checkbox remains the same. Here is the link to the JSFiddle for testing: http://jsfiddle ...

What is the best way to zoom in on an image and make it move off the screen as I scroll through my webpage?

I am working on a website design where I would like to implement an image zoom effect as the user scrolls down. Additionally, I want the image to move to either the left or right side of the screen as it goes out of view when scrolling to the bottom. While ...

Only display the child component once the asynchronous function in the parent's mounted hook has finished executing

In my coding setup, there is a parent component that is responsible for rendering a child component. The challenge I am currently facing involves making an asynchronous call (specifically an axios 'get' request from Vuex actions) inside the mount ...

Having difficulty positioning menu items beside the logo

I am facing an issue with aligning my menu items next to the logo in my navbar. Currently, the menu items are positioned slightly below and to the right of the logo. Although the horizontal alignment is correct, I need to adjust the vertical positioning to ...

Guide on converting a timestamp output in Node.js

I ran a query to retrieve TimeStamp data from MYSQL using Node JS, and the output is as follows: MyDate: Thu Apr 28 2016 07:02:45 GMT+0700 (SE Asia Standard Time) Can anyone help me convert it to yyyy-mm-dd hh:mm:ss format? ...

Transmitting JSON data to PHP for retrieval

( [addF] => stdClass Object ( [0] => stdClass Object ( [productID] => 33 [fQty] => 11 [fPW] => 11 [fP] => [fH] => PVC ...

Trouble with Add To Cart feature in React app due to issues with Context API

I have been following a tutorial located here. I followed the steps exactly and even checked the code on the related github repository, which matches. However, when I try to add a product to the cart by clicking the button, the state does not update. In Re ...

What is the reason behind the availability of the C# and ECMAScript ISO standards for free, while C/C++ standards are not

As someone deeply interested in the ISO C and C++ standards, I am intrigued by the fact that programming language standards for ISO/IEC 23270:2006 C# and ISO/IEC 16262:2011 ECMAScript can be accessed publicly on the ISO website. Meanwhile, standards for C ...

Using the spread operator to pass properties in React

Update: After delving deep into the documentation, @wawka has discovered that there may be some issues with the react-router-dom v^5.0.1 causing problems with the myLink2 component. It seems like a rewrite of this component may be necessary. In my React p ...

Customize the height of the Angular Material autocomplete dropdown to meet your needs

Context I have been working on a customized autocomplete feature using Angular Material, and I need to adjust the height of the dropdown results box for better visibility. Exploration After conducting some research, I discovered that Angular Material cu ...

What are some techniques for concealing a CSS transition beneath another element in both directions?

Witness the magic in action! By clicking on the black box below, a larger black box will gracefully emerge from beneath the sidebar. While it may not be as clear in jsfiddle, rest assured that this effect is more pronounced in browsers. Thanks to some clev ...

Generating PNG images with text using Node.js

I am currently working on generating a PNG file to be sent to clients through HTTP as an image/png response type. This new file will be created by combining 3 base PNG files and inserting custom text in the center of the image. Unfortunately, I have not ...

magnetic container: stationary container nested within absolutely positioned container

I recently created a page that can be viewed here: [LINK] This page is set up to scroll horizontally, resulting in a row of divs with black borders. However, I am facing an issue with the smaller divs inside (red ones). I want them to stay within the par ...