Magic Lasso
Magic Lasso

Reputation: 1542

Javascript: set element.style = element.style breaks the styling?

Is there anyway to just store the current style state of an element, so i can botch up the styling then jsut reset it?

Something similar to (although this doesnt work): http://jsfiddle.net/843Pj/

var el=document.getElementById('test');

var st=(function(s){
    var r=function(){return s;};
    return r;
}(el.style));

el.style.backgroundColor='blue';

el.style=st();

alert(el.style);

Upvotes: 2

Views: 92

Answers (1)

cookie monster
cookie monster

Reputation: 10972

If you're talking just about the original inline styles, the use the .cssText property of the style object.

var st = el.style.cssText; // store it

el.style.backgroundColor = 'blue'; // change it

el.style.cssText = st; // reset it

http://jsfiddle.net/843Pj/4/

You could even store it on a data- attribute of the element for easy access later.

Upvotes: 2

Related Questions