Reverse the order of jQuery toggle animations

Looking for something specific:

How can I create a button that triggers a script and then, when the script is done, reverses the action (toggles)?

(I am currently learning javascript/jquery, so I am a beginner in this field)

Here's an example:

    <script>
            $(document).ready(function(){

                $("button").click(function(){
                    $("div").animate( 
                            {height: '250px'}, 
                            function(){ $("#hidden_txt").fadeIn(1000); } );
                });

            });
        </script>

<button>Start</button>

<div style="background:#98bf21; height:100px; width:100px; position:absolute;">
    <span id="hidden_txt" style="display: none">Hey, just checking !</span>
</div>


I have solved it using CSS class toggle, but:

  • When closing, the CSS classes must be deactivated in reverse order, first fading out and then returning the div to 100px height
  • CSS transitions do not work in IE9 and IE8 :/

(jQuery 1.12.4)

Answer №1

Here is the recommended JavaScript code snippet:

<script>

    var clickCount = 0;
    $(document).ready(function(){

        $("button").click(function(){
            if(clickCount == 0){

                $("div").animate(
                    {height: '250px'},
                    function(){ $("#hidden_text").fadeIn(1000, function(){
                        clickCount = 1;

                    }); } );

            }else if(clickCount == 1){

                $("#hidden_text").fadeOut(1000, function(){

                    $("div").animate(
                        {height: '100px'},
                        function()
                        {
                            clickCount = 0;
                        });

                });

            } 
        });

    });
</script>

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

Version 66 of Chrome on Mobile Browser now offers Autoplay functionality

My goal is to implement a feature where an image with a play icon is displayed, and when the user clicks on the icon, a video is loaded in an iframe within a popup window. Despite following the guidelines provided in this link, we are facing issues with ...

NodeJS Streams: Delay in data transfer with Readable.pipe()

My understanding was that Stream.Readable.pipe() would immediately pipe data as it receives it, but I'm facing unexpected results while trying to create my own streams. const { Writable, Readable } = require("stream"); const writable = new ...

Is it possible to utilize Webpack 5's ChunkGroup API with several entries?

I am encountering an error message when attempting to upgrade from Webpack 4 to Webpack 5. The error states: Module.entryModule: Multiple entry modules are not supported by the deprecated API (Use the new ChunkGroup API) I have searched for information o ...

What could be causing the network error that keeps occurring when attempting to sign in with Google using React and Firebase?

Having an issue with Google authentication using Firebase. I set up the project on Firebase console and copied the configuration over to my project. Enabled Google sign-in method in the console which was working fine initially. But, after 2 days when I tri ...

Arrange the labels and input fields within nested elements in a synchronized manner

My data is structured semantically as shown below: <div class="inputs"> <div class="top"> <h4>Top</h4> <label for="top-1">Label 1</label> <input id="top- ...

Navigating Error: net::ERR_CONNECTION while utilizing Puppeteer

I attempted to utilize a proxy from the following site: Below is my Puppeteer scraping code (deployed on Heroku), encountering an error at the .goto() method, as mentioned in the title: const preparePageForTests = async (page) => { const userAgent = & ...

Navigate to the subsequent page by choosing the appropriate radio buttons instead of using the traditional method of

My goal is to have the radio buttons automatically navigate to the next page when clicked, without the need for me to manually click on the "next page" button. If you are interested in accessing the application form, you can do so by clicking on this link ...

The CSS display:block property isn't functioning as intended

I'm currently working on a contact form using CSS and HTML, but I'm having trouble getting the boxes to display properly in a 'block' form (display:block). HTML: <section class="body"> <form method="post" action="index.php ...

Is it necessary to include html, head, and body tags in pages loaded via AJAX requests?

I'm in the process of developing a web page that uses AJAX to load content within tabs. The content for each tab is stored in separate HTML files. My question is, do these individual HTML files loaded via AJAX need to contain the full <html>< ...

Enhancing React-Admin with custom Material-UI theme overrides for targeted CSS selectors on a global scale

When working with React-Admin, I encountered a challenge in applying specific CSS code globally within my Material UI theme. As I set up my theme, I included the following lines in the overrides section: overrides: { "@global": { &quo ...

Accessing User Input Data with JQuery

Can someone help me figure out how to store the input value from a Materialize select form in HTML using a variable in javascript/jquery? Here is the HTML code for the form: <div class="input-field col s12"> <select> <option va ...

Issue: nodebuffer is incompatible with this platform

Currently, I am attempting to utilize the Shpjs package in order to import a Shape file onto a Leaflet map. According to the Shpjs documentation: shpjs Below is the code snippet I am working with: const [geoData, setGeoData] = useState(null); //stat ...

Real-time chart updates through REST API integration with JavaScript

I am searching for a JavaScript library that is capable of creating line charts and making calls to a REST API every few seconds (such as 5s or 3s) in order to update the line chart dynamically. I have found some libraries that support live chart updatin ...

Looking for a way to add a CSS effect that reveals another image or text when hovering over an image?

My company's website features 10 country flags that, when clicked, display the entire site in that country's language for the user's session. However, I want to take it a step further and have a small image of the respective country appear w ...

Issue with JAWS screen reader failing to announce aria-expanded status

My aim is to ensure that the collapsed state is accessible in both NVDA and JAWS, but I am encountering issues with it displaying properly in JAWS. Does anyone have any suggestions on how to rectify this? I have included images of the code and link list b ...

Tips for sending a PHP array to jQuery AJAX using the $.post method

Goal: I am working with a php array variable and I want to pass this array ($key and $value) through jQuery AJAX using $.post to another php page called next.php. PHP Array Variable: $issue['key01']='value01'; $issue['key02 ...

What is the method for executing code in HTML without needing a beginning or ending tag?

I have created a code that creates a shape which alternates between the colors green and blue, along with changing text from 'Hi' to 'Hello' when a button is clicked. Now, I am looking for a way to make this transition happen automatica ...

Holding off on completing a task until the outcomes of two parallel API requests are received

Upon page load, I have two asynchronous API calls that need to be completed before I can calculate the percentage change of their returned values. To ensure both APIs have been called successfully and the total variables are populated, I am currently using ...

Determine if a paragraph contains a specified value using jQuery

I need to only select the checkboxes that do not have a value in the paragraph. Some mistake seems to be causing all the checkboxes to be marked when I click the button (Value > 0). Can you spot where I went wrong? $("input[id*='btnValue']").c ...

Extract data from the HTML source code in JavaScript and transfer it to a personalized JavaScript variable within Google Tag Manager

Running an e-commerce website on Prestashop and facing an issue with data layer set up. Instead of a standard data layer, the platform generates a javascript variable called MBG for Enhanced E-Commerce implementation. <script type="text/javascript"> ...