0

I have the following:

<style>
 .el {color: blue;}
</style>
<div class="el">bla bla</div>
<div class="el">bla bla 123</div>

And i need to use JavaScript to change the color in .el. I know about document.styleSheets but that seems kind of crude, having to loop search for the class i want to change it that way. Isn't there some better (lighter) way to do this nowadays? Something like

document.stylesheets.getclass('.el').color = 'red';

Thanks.

Derek Henderson
9,7364 gold badges45 silver badges72 bronze badges
asked May 13, 2013 at 10:36
4
  • This could be a helpful page to you Commented May 13, 2013 at 10:39
  • Check out Setting CSS pseudo-class rules from JavaScript @ StackOverflow Commented May 13, 2013 at 10:40
  • You can change the class, I think it would be better idea. Commented May 13, 2013 at 10:42
  • "Isn't there some better (lighter) way to do this nowadays?" Nope.There isn't. Commented May 13, 2013 at 10:57

4 Answers 4

1

You're on the right track. The method you're looking for is getElementsByClassName, not getClass. It returns an array, and then you simply loop through the array and assign the new color.

var el = document.getElementsByClassName('el'),
 i;
for (i = 0; i < el.length; i += 1) {
 el[i].style.color = 'red';
}

Demo

P.S., obviously, this changes the style, not the stylesheet.

answered May 13, 2013 at 10:44
Sign up to request clarification or add additional context in comments.

3 Comments

@user2079305: if you need to do without looping, use jquery like $(".el").css('color', 'red');
Yes, you're right. There are so many browser compatibility issues with trying to change the stylesheet directly that it's not practicable. And it's not as if you can save the changed stylesheet on the server.
Well yes, after looking at alternatives, I think that this will be the best solution. Definitely better than stylesheet array crawl.
0

Try this:

var elem = document.getElementsByClassName('el');
 for (var i = 0; i < elem.length; i++) {
 elem[i].style.color = '#aaaaaa';
 }
answered May 13, 2013 at 10:52

Comments

0

Changing the stylesheet dynamically is possible, but not that simple. Here is an example using .little PHP and jQuerY:

https://stackoverflow.com/a/78293807/760777

answered Apr 10, 2024 at 10:43

Comments

0

You can have 2 stylesheets such as:

<link id="style_1" rel="stylesheet" type="text/css" href="style_1.css">
<link id="style_2" rel="stylesheet" type="text/css" href="style_2.css">

Then add disabled attribute with JS to the one you'd like to disable, so the other will take effect.

answered Apr 10, 2024 at 11:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.