Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to revert css properties in jQuery

In my jQuery function, I am animating a div element, so it moves 200px to right, receives a Moved class and some opacity:

if (!$(".block2").hasClass("Moved2")) {
    $(".block2").animate({
        "left": "+220px"
    }, "slow", function() {
        $.ajax
        ({url: 'play.php',
            data: {"var1": val},
            type: 'get',
            success: function(json) {
                if(!json.error) {$(".block2").removeClass("Moved2").css({left:"0",opacity:"1"});}
        }});
    } ).fadeTo("slow", 0.33).addClass("Moved2");
}

This works fine, but after it finishes, I want to have all properties and css back as they were before animation. But I don’t want to reload the page.

As you can see, now I do it this way:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$(".block2").removeClass("Moved2").css({left:"0",opacity:"1"});

It does what I want, but there might be more properties affected in another functions and I don’t want to change each element one by one. Is there a kind of UNDO for everything done by that jQuery sequence?

>Solution :

You can use removeAttr(), in this way, it will remove all inline styles added to class .block2

$(".block2").removeClass("Moved2").removeAttr("style")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading