Is it better to let the old flash message fade away before introducing the new one?

I'm a beginner when it comes to jQuery. I have a form that calculates and displays the result in a flash message. My desired behavior is as follows:

  1. When the form is submitted for the first time, the flash message with the results should fade in.
  2. For subsequent submissions, the old flash message should fade out before the new one fades in.

I'm having trouble achieving this because both the old and new results share the same id.

Currently, only the fade in effect is working properly.

JavaScript (jQuery):

$(function() {
  $(".btn-primary").click(function() {
    $("#diagnosis").fadeOut("slow");
  });
$('#diagnosis').delay(0).fadeIn('normal', function() {
  $(this).delay(2500);
  });
});

CSS:

#diagnosis { display:none; }

Answer №1

You have the ability to chain fadeIn and fadeOut animations in Jquery. It is important to note that you can also include a complete function within each animation, like this:

$('#result').fadeOut('fast', () => {})
. This guarantees that the result is only updated once it has completely disappeared, which will then be followed by the new result fading in immediately with the updated value. For further information, please refer to this link.

let arr = [4, 5, 6, 7, 8, 9, 10]

$('form').on("submit", (e) => {
  e.preventDefault();
  
  // simulate calculated result 
  let genIdx = Math.floor(Math.random() * 7);
  let resultVal = arr[genIdx];
 
  $('#result').fadeOut('fast', () => {
      $('#result').text(resultVal);
  }).fadeIn('fast');

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
  <body>
    <form>
      <p id="result"></p>
      <button type="subimit">Submit</button>
    </form>
  </body>
</html>

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

Tips for creating seamless image transitions using a toggle button

My transition effect is working smoothly, but I am having trouble setting it up for the image. How can I resolve this issue? I attempted the following: var lightsOff = document.querySelector("#lights-off"); var lightsOn = document.querySelector("#lights-o ...

Update the div content following a comment submission

I want to insert a comment and have it displayed immediately in a thread using ajax Below is the code: JSP: <div class="comments"> //This section displays the list of comments <logic:iterate id="comment" name="discussion" property="com ...

Utilizing a combination of HTML, AJAX, and PHP, transfer an HTML input array via AJAX to a PHP webpage

Despite trying numerous similar solutions, none of them have proven effective for my issue. Within a form, users can input an unspecified amount of select options that can be added as needed using AJAX. My goal is to post this array to a PHP page via AJAX ...

JavaScript takes the spotlight before CSS

Currently experiencing this issue in Chrome, although Firefox seems to be working fine so far. Here is a greatly simplified version of the problem: HTML: <div class="thumbnail"> <a href='#' id="clickMe">Click me!</a> </d ...

Locating the specific row associated with a cell using jQuery

I’m new to programming and currently learning JQuery. I have a query: if there is a button in a column attribute, and upon clicking the button in that cell, I want to retrieve the unique ID value for that cell. For example, if my attribute name is "app ...

Is there a delay when dynamically filling out an input field in a form with the help of the jquery .keypress() function?

I have a form that I want to populate dynamically with the values from another form. It's almost working as intended, but there seems to be a delay of some sort. For example, if I enter "ABCDE" in field1, field2 will only populate with "ABCD". It ne ...

Displaying Dates in an Express Application using EJS

I have a MySQL table with a column containing date data. I am building a webpage to display this data in a more user-friendly way. Currently, the displayed result looks like this: https://i.sstatic.net/lGamr.png I would like the dates in the column to ...

Ways to eliminate text following a string substitution

When running the code below with keys assigned to summer, spring, fall, and winter, the output for ins would be: ['req.body.summer, req.body.spring, req.body.fall, req.body.winter'] I need to eliminate the surrounding string from the replace co ...

Position a modal in the vertical center with scroll functionality for handling extensive content

Looking for ways to tweak a Modal's CSS and HTML? Check out the code snippets below: div.backdrop { background-color: rgba(0, 0, 0, 0.4); height: 100%; left: 0; overflow: auto; position: fixed; top: 0; width: 100%; z-index: 2020; } ...

React's useState Hook: Modifying Values

I just started learning about React and React hooks, and I'm trying to figure out how to reset the value in useState back to default when new filters are selected. const [apartments, setApartments] = React.useState([]) const [page, setPage] = React.us ...

"Enhance your forms with Ajax for seamless uploading and convenient text

My current challenge involves submitting a form that features multiple file uploads using ajax along with text input. I'm facing an issue where if a user successfully uploads all the files using ajax but forgets to enter their name, the form resets an ...

Using StencilJS to Incorporate CSS/SASS Styles from node_modules

I'm currently facing a challenge in importing a CSS file containing variables from a node_modules package. Despite trying to replicate the process outlined in stencil.config.ts, the builds continue to end up in a different location than intended, leav ...

Mastering the art of exchanging cross-domain ajax data in userscripts

My current approach involves using this code to store and retrieve ajax data from $.ajax({ /* send data */ url: "http://api.openkeyval.org/store/", data: "test-key-data=" + JSON.stringify([123,456]), dataType: "jsonp", ...

Divide the number by the decimal point and then send it back as two distinct parts

Let's tackle a challenging task. I have a price list that looks like this: <span class="price">10.99€/span> <span class="price">1.99€/span> My goal is to transform it into the following format: <span class="price">10< ...

Locating discord.js users who possess multiple roles

Here is my code in its entirety as I have received some confusion on other forums: I am working on creating a Discord bot that can list users with a specific role. The plan is to create an array of roles, compare user inputs to the array, and display user ...

How to Implement Horizontal Sorting with jQuery Using Various HTML Elements

Here is the current setup I have: http://www.bootply.com/sfLr2AJ7EU The issue I am facing is with moving the image (id) boxes horizontally. While I have managed to make it work vertically, attempting to do so horizontally has been unsuccessful. The image ...

Unable to Retrieve Stripe Connect Account Balance

I've been attempting to retrieve the balance for my connected accounts in Stripe, but every time I make an API call, it keeps returning the platform's account balance instead of the connected account balance. This is happening while in test mode. ...

What is the best way to display the Edit and Delete buttons in a single column?

Currently, I am encountering a small issue. I am trying to display Edit and Delete Buttons in the same column but unfortunately, I am unable to achieve that. Let me provide you with my code. var dataTable; $(document).ready(function () { dataTa ...

Guide to updating the value of an input field with Selenium in Python

I have an input element that I need to clear its current value and input a new one. The HTML structure is as follows: <input class="input-mini" type="text" name="daterangepicker_start" value=""> To locate this element, I used the following code: ...

The grid flex end is behaving differently than I anticipated

I am struggling to align two buttons vertically on the right side. Here is my code snippet: const WalletsContainer = () => { return ( <Grid style={{ background: 'red' }} direction={'column'} alignItems={'flex-end'} ...