1

Let's assume we have lots of elements (see Live demo):

<div class="yes">Hello1</div>
<div class="yes">Hello2</div>
<div class="yes">Hello3</div>
<div class="yes">Hello4</div>
<div class="yes">Hello5</div>

with the same class called yes which has a simple CSS style :

.yes { color:red; }

Now I would like to change the style of the yes class.

Of course, we can change CSS style of this class with Javascript by using

var yesarray = document.querySelectorAll('.yes');

for (var i = 0, len = yesarray.length; i < len; i++) {
    yesarray[i].style.color = 'black';
}

(or even more easily with jQuery, with $('.yes').css(...) ...)

But then all these changes will be stored in the DOM :

enter image description here

This is not very elegant if I have lots of elements, that all these styles are "rendered in the DOM" like this.

Question:

How to change CSS style with Javascript such that the style change is not stored in the DOM, but modified in the loaded-in-memory CSS instead?

7
  • You're doing it the right way, it doesn't matter if the styles are inline. Commented Nov 8, 2014 at 14:51
  • @adeneo it does matter for me, because 1/ I can have thousands of such elements, 2/ Later I want to serialize the DOM structure. I find that having a thousand of inline styles is inelegant / increases the serialized data. Commented Nov 8, 2014 at 14:53
  • If you have thousands of elements and you're going to serialize the DOM structure, you're probably doing something else wrong, as that seems unnecessary. You can of course append a style tag to the head instead, but that's really even worse practice, even if it soves the issue of having styles on each element. Commented Nov 8, 2014 at 14:56
  • 1
    Can you create all your classes before-hand and simply use javascript to change the class instead of CSS properties?(to avoid inline styles) Commented Nov 8, 2014 at 14:56
  • that's what I was doing in the meantime, and I think it may solve the problem @Schmalzy :) . If you want to post as an answer, I'll accept it. Commented Nov 8, 2014 at 14:59

2 Answers 2

4

You can't edit the CSS style page, but you can do something else.

You are doing in the right way but with a wrong approach.

You have to add ANTOHER style to each element with "yes" class. This style can be called in your example "approved"

So your jquery call will be:

EDIT: Schmalzy said correctly: $('.yes').addClass('approved');

equivalent of:

 $('.yes').each(){
  $(this).addClass('approved');
}

and previusly you declared in your stylesheet:

.approved {
 color: black
}

So in your page you will store classes (elegant and shorter) and not inline style.

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

2 Comments

No need for the .each() $('.yes').addClass('approved'); would do the same thing.
No need to add a class to all the elements. Add a single class to the <body> tag and use a CSS descendant selector to enforce the rule.
1

Instead of modifying every DOM node with class "yes", you can use the power of CSS descendant selectors to make the change a lot cheaper:

CSS:

.yes {
  color: blue; /* or whatever */
}

.approved .yes {
  color: black;
}

Then in JavaScript:

$("body").toggleClass("approved", shouldApprove());

Only one DOM node is updated (though, of course, the browser still has to recompute the layout of the whole page).

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.