0

Aside from doing property = blank like the example below, is there a way to drop all css changes made by a script without refreshing the page? Something like re-loading the original stylesheet?

$("#div".css({property1:"", property2:""});

I'm currently studying JavaScript and jQuery and I want to implement some sort of "reset" button to my page, which in some parts, color and size can be modified by the user.

3
  • I guess you need to keep track of the CSS you change with JavaScript somehow and use that to reset everything. Commented Dec 20, 2014 at 13:53
  • I think you should keep track of changes for further use. actually you need to know what has been changed. Commented Dec 20, 2014 at 13:53
  • You'll have to store all changes in an array with your preferred choice of manner and then create a function that iterates over each once, parses it, and undoes it. Commented Dec 20, 2014 at 13:56

3 Answers 3

4

Well, if all your original styles were loaded in a style sheet and all your changes were done via jquery (which applies the change directly to the style attribute), then one easy way to do it is:

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

If you want to apply it to all things with such a modification then you could use the attribute selector:

$('[style]').attr('style','');
Sign up to request clarification or add additional context in comments.

4 Comments

Hi! Just wanted to make sure I understood it. $('#div').attr('style',''); is a more specific reset, it targets a div. while $('[style]').attr('style',''); targets the stylesheet directly and removes all styles?
No, the stylesheet never gets modified by this command. What it does is set the element's style to empty. The difference between the two is that the first one targets only one element of the dom by id (#div), whereas the second one ($('[style]')) targets all elements with style attributes in the dom.
@Marventus I understand that, what he said he was looking for was a way to remove all the changes made via jquery using jquery.css(), not that he was trying to modify the spreadsheet.
I know, Paul, ;-). I was answering to Michael's comment. My bad: I should have prefixed it with @Michael.
3

Like mentionned in previous answer, you can simply overwrite the style attribute with blank.

But, you can also remove it:

$("Your selector").removeAttr("style");

Comments

0

If your element does not contain any other inline styles set by other functions, you could just set the default styles in your CSS style sheet, and remove the "style" attribute to remove its inline properties added through JS:

Pure JS:

var styled = document.querySelectorAll("*[style]");
for(i=0; i<styled.length;i++) {
    styled[i].removeAttribute("style");
}

jQuery:

$("*[style]").removeAttr("style");

Explanation:

Both approaches will remove inline styles from all the elements in the document.

Demo:

Here's a JSfiddle as a demo.

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.