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?
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.
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.
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
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:
:root { --some-padding: 12px; } .someClass { padding: var(--some-padding); }
// 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%');
(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); }
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With