3

So I want to get the css from a particular Element, put it in JSON-array, change element around, later restore the css of the element from the stored Array.

I tried:

var currentCSS = $(this).css;

The output is something like:

function (a,c) if(arguments.length....

So that seems it takes the function out of the jQuery, that's not what I want...

I could iterate through the wanted individual arguments, but there should be a better way...

Then later offcourse I'd try something like:

$(this).css(currentCSS);

But there might be no elegant solution to doing this...

2

3 Answers 3

2

If all you are after is inline style as opposed to all of the style properties:

var $el=$(elem)
var style=$el.attr('style');
$el.removeAttr('style');


/* put style back*/

$el.attr('style',style);

Similarly after you have manipulated it, removing any inline adjustments that your code makes ... removing style attribute should put it back to whatever original css was applied

Sign up to request clarification or add additional context in comments.

3 Comments

That will not get them the full style. That will only get those attributes unique to the object.
@Elf Sternberg - understood...but possible that is all that is needed, was offfering a suggestion
I think this does exactly what he's looking for. This is similar to an approach I took in this answer (but he wanted the whole page): stackoverflow.com/a/11328023/918414
1

Here's one way, but goodness it's ugly. I don't think I have the sanity required to turn this into a plugin (but if someone else could that'd be awesome! Please post a link!):

var elem = document.getElementById('a');
var originalCSSProperties = window.getComputedStyle(​elem,null);
var originalCSSValues = [];
for (var i=0,len=originalCSSProperties.length; i<len; i++){
    originalCSSValues.push(
        window
        .getComputedStyle(elem,null)
        .getPropertyValue(originalCSSProperties[i]));
}

$('#a').css({'background-color':'red','color':'black'});

$('#reset').click(
    function(){
        for (var c=0,leng=originalCSSProperties.length;c<leng;c++){
            $('#a').css(originalCSSProperties[c],originalCSSValues[c]);
        }
    });

JS Fiddle demo.

Because this relies on window.getComputedStyle() it's absolutely not IE friendly, and, while I've not researched compatibility yet, I suspect it might require pretty modern browsers in general. But, that said, I hope it's of some use.

Comments

0

To start with:

  • Create a clone of your element with jQuery.clone and store it in a variable
  • Remove any child elements from the clone
  • Make any needed changes you want to your element or it's contents

When you want to change back to your original style

  • Insert the clone before/after your element in the DOM
  • Remove the contents from your element and append them to the clone
  • Remove your element leaving only the clone with your element's contents

Of course there is no JSON array here where the styles are stored. Instead, your original styles are preserved in the clone of your element. I think this achieves the desired result you want anyway.

Here's a JSFiddle I made showing this in action.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.