Using JQuery to reverse a function

Seeking a solution here: is it possible to revert a function using jQuery? I have a click function with multiple actions within. Is there a way to revert these elements back to their original state before the click event? Thank you for any assistance provided.

$('.bottom_panel_button_02').click(function(){
    $('#recipe_panel').css('opacity','1');
    $('.default_clock, .info_top_panel .random_title, .bottom_panel_button_06').css('display','none');
    $('.bottom_main_panel_button').css('background','url("img/ap_bottom_panel_button_white.png") center bottom no-repeat')
    setTimeout(function(){
        $('.arrow_up, .arrow_down').fadeIn(300);
    },500);
    $('.main_content, .oven_panel').fadeOut(200);
});

Answer №1

Unfortunately, jQuery does not have a built-in feature to easily revert a series of DOM manipulations or animations. To achieve this, you will need to create two separate functions that act as mirrors of each other and manually reset the changes made in the initial action:

function applyChanges(){
    $('#recipe_panel').css('opacity','1');
    $('.default_clock, .info_top_panel .random_title, .bottom_panel_button_06').css('display','none');
    $('.bottom_main_panel_button').css('background','url("img/ap_bottom_panel_button_white.png") center bottom no-repeat')
    setTimeout(function(){
        $('.arrow_up, .arrow_down').fadeIn(300);
    },500);
    $('.main_content, .oven_panel').fadeOut(200);    
}

function revertChanges(){
    $('#recipe_panel').css('opacity','0');
    $('.default_clock, .info_top_panel .random_title, .bottom_panel_button_06').css('display','block');
    $('.bottom_main_panel_button').css('background','')
    setTimeout(function(){
        $('.arrow_up, .arrow_down').fadeOut(300);
    },500);
    $('.main_content, .oven_panel').fadeIn(200);    
}

$('.button_to_apply_changes').click(applyChanges);
$('.button_to_revert_changes').click(revertChanges);

Answer №2

One way to achieve this is by utilizing custom attributes:

$(document).ready(function() {
    $('.bottom_panel_button_02').attr("clicked", "false");

    $('.bottom_panel_button_02').click(function() {
        var clicked = $(this).attr("clicked");

        if (clicked == "false") {
            // implement the click functionality

            $(this).attr("clicked", "true");
        } else {
            // implement the revert functionality

            $(this).attr("clicked", "false");
        }
    });
});

Alternatively, you could maintain a global variable or utilize a hidden input element to keep track of the state instead of using attributes. Another approach would be to use .addClass and .removeClass based on the clicked state of the element and define your states within these CSS classes.

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

The retrieved information remains unchanged even after modifications are made on the subsequent Next.js server-side rendered page

I'm facing an interesting scenario with my application. It consists of two main pages - one displaying user account statistics and the other allowing users to edit these statistics. Both pages are rendered on the server side. When I navigate to the fi ...

Is it possible to download multiple files using a single ajax call upon success of a click function?

In my file, there are 2 anchor tags with their respective href links. I am triggering both anchor tags at ajax call success. <a id="exportExcelFatturaIcon" href ="${createLink(action: 'downloadExcel', params: [fileName:excelFileName])}" hidde ...

Using AjaxView in Django to retrieve choices dynamically

In the form I am working on, there are two ChoiceFields. The choices available in the second field are dependent on the selection made in the first field. I have implemented an onchange event in the template: $("#id_department").change(function(){ con ...

"Building a dynamic form with ReactJS, Redux Form, and Material UI - Implementing an

Currently working on a nested form framework that utilizes the redux form and material UI framework. The components have been developed up to this point - https://codesandbox.io/s/bold-sunset-uc4t5 My goal is to incorporate an autocomplete field into the ...

How to make text in HTML and CSS stay at the bottom of a div?

I am in the process of creating a website dedicated to my "Starcraft II" clan. My goal is to have the navigation bar display the text "ALLOYE" and remain fixed at the bottom of the screen. I attempted to achieve this using the following code: vertical-alig ...

What is the best method for transferring array values from an input field to a function when the input

When selecting dates, the format that comes out is 02/01/2017-06/19/2017 as one value. However, I need to pass these selected values as two separate values to a function named "VizDateRange". How can I achieve this? //HTML <div class="input-group date ...

How to hide an item in Ionic 2 using navparams

Is there a way to dynamically hide or show a list item on page load depending on certain parameters? Here is an example of the code I am currently using: HTML <button ion-item (tap)="goToPage2()" [hidden]="shouldHide">Page 2</button> TS ex ...

Saving user-generated inputs within a React.js component's state, focusing on securely handling passwords

Curious about forms in React.js? I don't have any issues, but I'm wondering if there are potential flaws in my approach. I've set up a basic form with two inputs for email and password: <input type="email" name="email" value= ...

The tooltip for the svg icon is not appearing

Is there a way to apply the tooltip from this tutorial to an SVG info icon like in this codepen? I tried adding the classes to the svg, but the tooltip doesn't show. How can I make the tooltip work on the SVG info icon? Thank you. body { backgrou ...

What is the method for including an input field beside the y-axis label in Chart.js?

I'm struggling to implement a live poll using Chart.js where users can select their option by checking a checkbox next to the y-axis label. My initial attempt was unsuccessful as placing the input boxes outside of the canvas led to alignment issues wi ...

Sending the parameter with the URL and receiving the response in return

Can the result be retrieved by calling a URL and sending specific parameters with it? For instance, if I pass two numbers along with the URL, can I receive the sum in return? The addition operation is carried out on the page being called upon. ...

Positioning nested DIVs using HTML5 and CSS3

/* HTML */ <header role="heading"> <div class="logo"> <h1><a href="/">this is the banner</a></h1> </div> <header> /* CSS */ header { background-color: rgb(255, 165, 0); height: 160px; ...

Testing for undefined using the 'should' syntax in Chai

Inspired by this tutorial on testing an AngularJS app with Chai, I am trying to add a test for an undefined value using the "should" style. However, I encountered a failure when attempting this: it ('cannot play outside the board', function() { ...

What is the process of connecting data to a list in Angular JS?

My HTML code includes a text box and two buttons: 'Go' and 'Clear Completed'. When I enter text in the textbox and click on the 'Go' button, it should be added to an unordered list. However, I am facing an issue where unexpect ...

Using CSS to crop an image with a combination of relative and absolute positioning

I'm working with the following HTML and CSS (Check out the Fiddle Example): header { text-align: center; } div.wrapper { margin: 0 auto; max-width: 640px; text-align: left; position: relative; } em.brand img { display: block; } ul.me ...

Is there a way to delete cookies upon browser closure, but retain them while moving between pages?

Hello, I am looking for a way to clear all cookies and local storage when the browser is closed, while still retaining them while navigating between pages in Next.js. I have tried using the window.onbeforeunload event, but this also triggers when navigat ...

jQuery is an excellent tool for implementing drag and drop folder upload functionality, all without

I am creating a drag and drop file uploader with basic functionality. Here is the code: HTML: <div class="drop-box drop-area"> <form enctype="multipart/form-data" id="yourregularuploadformId"> <input type="file" name="files[]" ...

Prevent handling errors in Vue when Axios receives a response for every request

In my project, I utilize axios interceptors to manage various errors, particularly those without a response. However, I rely on the error.response.data message for validations and displaying messages stored in the backend in certain areas of the project. D ...

How can I verify the presence of links in an input field and determine its character count?

I've dived into coding for this project and I'm making progress. Here are my objectives: 1) Verify the length of URLs entered in a field and reduce each link's length by 20 if it exceeds 20 characters. 2) Calculate the remaining characters i ...

The click event in AngularJS is functioning properly, while the load event is not functioning correctly

As a newcomer to angularjs, I am attempting to run a function once a div has finished loading. app.js var app = angular.module('myApp',[]); app.directive('orientable', function () { return { link: function(scope, e ...