0

I want to change certain properties in a CSS class dynamically, i.e. modify a class dynamically. Since there is no method or function in Jquery to do so, I think it would be easy if one can get the CSS properties in a class, then modify the ones which need a change and copy the rest verbatim. If this is possible, then a removeClass() and addClass() will suffice to implement a modifyClass() method.

Sunny
  • 9,245
  • 10
  • 49
  • 79
  • 1
    What you're looking for seems totally useless! Why would you have to change a class dynamically when you can just add another class or any inline style of your choosing. As a sidenote, the easiest way to achive this would be to create an element with that class and get the computedStyle, and to change an entire class you could insert a style tag at the bottom of the head section, but why would you ? – adeneo Dec 28 '13 at 19:18
  • I want to "modify" the styles dynamically through the life of the session and store the class on disk to be reused later. Adding a series of new classes each time would not be the most efficient way to do this as the class gets changed several times... during a session. I will have to save all those classes. Adding new classes is exactly what I am doing now but I need to store the "final" class (i.e. all properties with the hand-coded static class and all properties in the dynamically generated classes applied on that element). – Sunny Dec 28 '13 at 19:35
  • Are you serious, save a dynamically altered class to disk over the span of a session? Good luck with that! – adeneo Dec 28 '13 at 19:41
  • I am already doing it and it works but is not elegant. All dynamically added classes and CSS properties are written into session along with a timestamp and at the end of the session, all the properties are sorted and in case of duplicates the most recent one is selected for saving. The application is complex but provides continuity across sessions in more than one way. I am giving a nutshell scenario here. – Sunny Dec 28 '13 at 20:07
  • Wouldn't it be sufficient to keep track of just one class with overrides/additions to the base class? Because that's not so hard. -- as a side-note, in general it is a bit odd to speak of css properties of a class, because `div.someclass` can have entirely different properties than `span.someclass`. – towr Dec 28 '13 at 21:44
  • @towr. If it was just one element, then you are right. I am looking for a generic solution. – Sunny Dec 29 '13 at 04:07
  • You can [get all css for an element](http://stackoverflow.com/questions/754607/can-jquery-get-all-css-styles-associated-with-an-element), so to get the properties for a `class` you could subtract the css for an element without the class from the same element with the class. – towr Dec 29 '13 at 19:39

0 Answers0