Switching the content of a button using jQuery and then reverting it

In my quest for a simple feedback mechanism, I want to create an effect where pressing a button will quickly fade it out, change its class and content, then fade it back in before reverting to its original state.

Here is the code snippet that I believed would accomplish this task:

$('#event1-btn-451').fadeOut(200).toggleClass('btn-primary btn-success').html('Success!').fadeIn(200);
$('#event1-btn-451').delay(2000).fadeOut(200).toggleClass('btn-success btn-primary').html('Add Event').fadeIn(200);

While the first line works as expected, including the second line causes the button to simply fade out, then fade in with the 'Add Event' text in its original color, repeating the process after a 2-second delay.

JSFIDDLE

This is my first time posting here, although I have found answers on this platform countless times before!

Answer №1

To create this specific type of animation, it is important to connect them in a sequence, as shown below:

View the animation on jsFiddle

$('#event1-btn-451').click(function () {
    $(this).fadeOut(200, function(){
        $(this).toggleClass('btn-primary btn-success')
            .html('Success!')
            .fadeIn(200) // This step does not need to be chained because there is a delay
            .delay(2000)
            .fadeOut(200, function(){
                $(this).toggleClass('btn-success btn-primary')
                    .html('Add Event')
                    .fadeIn(200);
            })
        })
});

Answer №3

Here's a suggestion to try out:

$('button#event-btn').click(function () {
    var _elem = $(this);
    _elem.fadeOut(200, function () {
        _elem.toggleClass('active inactive').html('Completed!').fadeIn(200).delay(3000).fadeOut(200, function () {
            console.log('d');
            _elem.toggleClass('inactive active').html('Add Event').fadeIn(200);
        })
    });
});

Utilize the callback function of jQuery's fadeOut() method

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

Creating interactive HTML cards by dynamically generating them from a Javascript array and Bootstrap, organizing them into distinct rows and columns

I need assistance in structuring my Team section with multiple rows, each containing 5 cards. I have a list of team members that I want to display in this format using a single array and loop. However, I am struggling to figure out how to automatically cre ...

Creating a div resizing function using only JavaScript is a fun and useful project to tackle

Seeking help to create a div resizer using only pure JavaScript as jQuery is restricted. The current functionality works, but breaks when the position of the slider-div is not set to absolute. Any solutions to fix this issue would be greatly appreciated. ...

Can a websocket be used to communicate with a server that is not the same as the one hosting the html5 website?

My goal is to establish communication between a hardware device and an HTML5 website. It seems like the best way to do this would be through WebSockets or possibly WebRTC over a WiFi network. Is that correct? I haven't come across any solutions invol ...

"Revealing the specific row and column of the selected element in each table when hovering over it

On my webpage, I have set up two tables positioned side by side like this: <!DOCTYPE html> <table> <tr> <td> <table> <tr><td>1</td><td>2& ...

Eliminating unnecessary gaps caused by CSS float properties

I need help figuring out how to eliminate the extra space above 'Smart Filter' in the div id='container_sidebar'. You can view an example of this issue on fiddle http://jsfiddle.net/XHPtc/ It seems that if I remove the float: right pro ...

Images on WordPress are automatically resized for optimal display

I'm facing a puzzling issue with my company's wordpress website. Despite setting the dimensions of a 200x140 image correctly in the HTML code, it seems to be scaling up and taking over the entire page. I've double-checked everything, from th ...

perform one task following the completion of another

Can I run the second function after the first one without using a timeout event in this JavaScript code: if (direction !== "leftnav") { // DO THINGS }; setTimeout(function () { //DO OTHER THINGS AFTER THE FIRST THING }, 1000); Your input is greatly app ...

What is the best way to utilize a jQuery selector within a (tornado) template tag?

Just hoping this question hits the mark. So, I have a Python/Tornado app and I'm struggling with populating two combo boxes on my HTML page. The goal is to populate the second combo box based on the selected value in the first one. Here's the str ...

Set Blueprint container with a full-width background that fills the entire browser window

I'm currently utilizing the Compass framework in conjunction with the Blueprint framework. Within my setup, I've configured a 24 column layout with a total width of 1000px declared in my base.scss file. The HTML structure appears as follows: & ...

My image is being cropped on larger screens due to the background-size: cover property

I am currently in the process of designing a website with a layout consisting of a Header, Content, and Footer all set at 100% width. To achieve a background image that fills the screen in the content section, I utilized CSS3 with background-size:cover pr ...

swapping out an input parameter

Looking for a more efficient way to replace a string in a textarea without causing issues with the script due to "&" characters? function doRequest(){ var str = $('textarea#tr').val(); $.ajax({ type: "POST", url: "index.php?sd=t", d ...

implementing datepicker on multiple textboxes in jQuery upon button click

I have the ability to show multiple text boxes using jQuery. I am now looking to add a datepicker to those text boxes. Any assistance in finding a solution would be greatly appreciated. Thank you in advance. ...

Using jQuery to create a flawless animation

I am currently working on an animation project, and I have shared my progress on jsfiddle. Below is the code snippet I have utilized: /* JavaScript: */ var app = function () { var self = this; var allBoxes = $('.box&apos ...

Ending the Overlay

I am using an overlay: <div id="overlayer" class="overlayer"> <div id="board" class="board"></div> </div> Here are the CSS properties I have applied to it: #overlayer { position:fixed; display:none; top:0; left:0; width:100%; hei ...

Error: Disappearing textarea textContent in HTML/TS occurs when creating a new textarea or clicking a button

I've encountered an issue with my HTML page that consists of several textareas. I have a function in place to dynamically add additional textareas using document.getElementById("textAreas").innerHTML += '<textarea class="textArea"></text ...

Achieving the perfect horizontal alignment of images

Currently working on a new project and here's the page I'm focusing on: The elements are exceeding the set height of their container and overlapping into the content area instead of pushing the existing content down. Any suggestions to fix this ...

Calculate the number of times each date appears in the array, grouped by hour

I have a method that does the following: // Push dates into dateArray var dateArray = []; $.each(dateHtml, function (i, el) { dateArray.push(htmlDateValue); } }); Next, I sort the array by the earliest date: // Sort arra ...

Utilizing multiple jQuery versions concurrently within a single webpage

Currently, I am working on my project using Twitter Bootstrap. In order to incorporate a file upload script extension with jQuery, I have come across an existing plugin that is already developed and simply needs to be integrated. The issue at hand is that ...

Is there a way to show a progress bar that functions as the background for a table row

My table is structured as follows: th1 th2 th3 th4 th5 th6 td1 td2 td3 td4 td5 td6 td1 td2 td3 td4 td5 td6 I am looking to incorporate a feature where new rows are dynamically added using a form that triggers ...

IMG creates scroll bars even when width is set as percentage

In a previous discussion about how padding is calculated when using percentages (%), it was mentioned that the padding is determined based on the parent element's width. Now, I have come across the following CSS assignment: .img_tutorial_full_width ...