It’s not possible to directly access pseudo-elements with Javascript as they’re not part of the DOM. You can read their style using the optional second argument – which most, although not all, browsers in current use support – in .getComputedStyle()
but you can’t directly change their style.
However, you could change their style indirectly by adding in a new style
element containing new rules. For example:
http://jsfiddle.net/sjFML/
The initial CSS assigns the :before
pseudo-element with a green background, which is turned to black by inserting a new style
element.
HTML:
<div id="theDiv"></div>
CSS:
#theDiv {
height: 100px;
background: red;
}
#theDiv:before {
content:' ';
display: block;
width: 50px;
height: 50px;
background: green;
}
Javascript:
var styleElem = document.head.appendChild(document.createElement("style"));
styleElem.innerHTML = "#theDiv:before {background: black;}";