Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - changing a class' style

I've been working with jQuery for a while, but now I want to write something in pure javascript and it's prooving to be challenging.. One of my biggest problems at the moment is that I haven't found a way to set/change styling for a class. This is not a problem for elements with id, but I want to change the styling for a group of elements with the same class and not just for one element with an id.. In jQuery I would just write:

$('.someClass').css('color','red') 

Is there really no simple equivalence to this in pure js?

like image 581
user1231410 Avatar asked Feb 24 '12 18:02

user1231410


People also ask

Can I change CSS class in JavaScript?

JavaScript offers us two properties that we can use to modify CSS classes; classList and className property. The className property is used to set a value to a CSS class directly whereas the classList gives us some built-in methods to manipulate the CSS classes.

How do you change classes in CSS?

class selector selects elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the name of the class.


2 Answers

Try the following

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

Note: As Cheery pointed out getElementsByClassName won't work in IE. The linked question has a nice way to work around this limitation

  • javascript document.getElementsByClassName compatibility with IE
like image 191
JaredPar Avatar answered Sep 28 '22 19:09

JaredPar


I find it easier to use CSS variables. You can set the class to use a variable and then change that value in Javascript, thus changing the CSS.

If you style the class like:

:root {     --some-color: red; }  .someClass {     color: var(--some-color); } 

Then you can change the variable's value in Javascript with

document.documentElement.style.setProperty('--some-color', '(random color)'); 

(random color) can then be anything that would be considered a valid CSS color (eg. blue, black, #626262, rgb(12, 93, 44))

Updating the value in JS automatically updates the page as well.

And of course, this can be done with any property, not just color. Here is an example that changes the padding of a class:

CSS

:root {     --some-padding: 12px; }  .someClass {     padding: var(--some-padding); } 

Javascript

// Set padding to 15px document.documentElement.style.setProperty('--some-padding', '15px');  // Set padding to 5rem document.documentElement.style.setProperty('--some-padding', '5rem');  // Set padding to 25% document.documentElement.style.setProperty('--some-padding', '25%'); 

Useful example: toggle dark / light mode:

(How to use css properties to dynamically set css properties)

// set to light mode: document.documentElement.style.setProperty('--bg-color', getComputedStyle(document.documentElement).getPropertyValue('--bg-color-light'));  // set to dark mode: document.documentElement.style.setProperty('--bg-color', getComputedStyle(document.documentElement).getPropertyValue('--bg-color-dark')); 

With the respective css:

:root {   --bg-color: black;   --bg-color-light: white;   --bg-color-dark: black;  body {   background-color: var(--bg-color); } 

Sources

How to declare and use CSS variables: https://www.w3schools.com/css/css3_variables.asp
How to update a CSS variable in JS: https://css-tricks.com/updating-a-css-variable-with-javascript/

like image 25
Eliatiscom Avatar answered Sep 28 '22 20:09

Eliatiscom