views:

37

answers:

2

After a bunch of animating, adding classes and setting css styles. is there an easy way to reset an element to be back in its original server delivered state?

A: 

I don't think it's possible to revert the state.
But you could load the page again with ajax and replace everything with the fresh copy

The Disintegrator
most definitely not an option as a lot of the other elements have to be in their modified state.
Moak
I misread your question. O though you wanted to reset everything.
The Disintegrator
+2  A: 

jQuery sets its manipulations using the inline style attribute, so just set that to ''.

$('#someDiv').attr('style','');

This assumes there were no inline style attributes set on the element coming from the server. Better to use style sheets if so.

If the element must come from the server with style attributes set, then I suppose you could cache the value in a variable, and reset it when needed.

   // Cache original attributes
var originalAttributes = $('#someDiv').attr('style');


   // Reset from original
$('#someDiv').attr('style',originalAttributes);

EDIT:

If needed, you could send your elements from the server with custom attributes to remember the original class attribute, for example.

<div class="myClass" originalClass="myClass">...</div>

Then you can reference the original anytime you need.

You could even find all elements that have the originalClass attribute like this.

var $elementsWithOriginal = $('[originalClass]');

or find elements where the class attribute has been modified from the original.

var $modifiedFromOriginal = $('[originalClass]').filter(function() {
        return $(this).attr('class') != $(this).attr('originalClass');
    });
patrick dw
seems allright, what about the memorizing of what class it used to have and not have?
Moak
@Moak - Maybe try something a little creative like using a custom attribute like originalClass, so your element would come from the server like this: `<div class="myClass" originalClass="myClass">...</div>`. You can then get the value of originalClass just like any other attribute.
patrick dw
@Moak - I updated my answer with examples.
patrick dw
Wouldn't .data() be a better way to store the originalClass?
Felipe Alsacreations
Ancestors could have style and class attributes from which CSS properties will be inherited: I don't think that question has an easy answer :)
Felipe Alsacreations
@Felipe - `data()` would be an alternative, but no, not necessarily better. `data()` is important when you need to store a type other than a String. A class name is a mere string, so there's no special advantage to using `data()`.
patrick dw
@Felipe - Also, the question was about resetting elements that have been styled by jQuery back to their original state. The presumption is that the OP was aware of styling effects with regard to ancestry when applying the styles, and will be aware of the effects when removing jQuery generated styles.
patrick dw