Change the style of the class (not the individual elements) that is not in the stylesheet using Javascript

I have a series of divs that share a class for various reasons (and several divs that are created using code at other points).

I tried to find the answers, but everyone relies on a class that is in the stylesheet (if there is one), which does not happen in this case or that it modifies existing elements instead of existing rules.

So I need to do something like ...

var add_to_styles('myClassName');
myClassName.cssText = "color: red;"

And all created divs (both before and after the script) that have a class

<div class="myClassName">I should be red</div>

Exit with rule change. But I did not find anything like it.

+4
source share
3 answers

<style></style> ( ) ( ).

( , ).

+2

jQuery, .css().

https://jsfiddle.net/rixlabs/annt81df/1/

$(".myClassName").css("color", "red");
+1

Try the following:

document.getElementByClassName('myClassName').style.color = "red";

or

document.getElementById('myIdName').style.color = "red";
-1
source

All Articles