Is there any way to select/manipulate CSS pseudo-elements such as ::before
and ::after
(and the old version with one semi-colon) using jQuery?
For example, my stylesheet has the following rule:
.span::after{ content:'foo' }
How can I change 'foo' to 'bar' using jQuery?
IF you want to to manipulate the ::before or ::after sudo elements entirely through CSS, you could do it JS. See below;
Notice how the
<style>
element has an ID, which you can use to remove it and append to it again if your style changes dynamically.This way, your element is style exactly how you want it through CSS, with the help of JS.
Here is the HTML:
Computed style on 'before' was
content: "VERIFY TO WATCH";
Here is my two lines of jQuery, which use the idea of adding an extra class to specifically reference this element and then appending a style tag (with an !important tag) to changes the CSS of the sudo-element's content value:
$("span.play:eq(0)").addClass('G');
$('body').append("<style>.G:before{content:'NewText' !important}</style>");
You'd think this would be a simple question to answer, with everything else that jQuery can do. Unfortunately, the problem comes down to a technical issue: css :after and :before rules aren't part of the DOM, and therefore can't be altered using jQuery's DOM methods.
There are ways to manipulate these elements using JavaScript and/or CSS workarounds; which one you use depends on your exact requirements.
I'm going to start with what's widely considered the "best" approach:
1) Add/remove a predetermined class
In this approach, you've already created a class in your CSS with a different
:after
or:before
style. Place this "new" class later in your stylesheet to make sure it overrides:Then you can easily add or remove this class using jQuery (or vanilla JavaScript):
:before
or:after
isn't completely dynamic2) Add new styles directly to the document's stylesheet
It's possible to use JavaScript to add styles directly to the document stylesheet, including
:after
and:before
styles. jQuery doesn't provide a convenient shortcut, but fortunately the JS isn't that complicated:.addRule()
and the related.insertRule()
methods are fairly well-supported today.As a variation, you can also use jQuery to add an entirely new stylesheet to the document, but the necessary code isn't any cleaner:
If we're talking about "manipulating" the values, not just adding to them, we can also read the existing
:after
or:before
styles using a different approach:We can replace
document.querySelector('p')
with$('p')[0]
when using jQuery, for slightly shorter code.3) Alter a different DOM attribute
You can also to use
attr()
in your CSS to read a particular DOM attribute. (If a browser supports:before
, it supportsattr()
as well.) By combining this withcontent:
in some carefully-prepared CSS, we can change the content (but not other properties, like margin or color) of:before
and:after
dynamically:JS:
This can be combined with the second technique if the CSS can't be prepared ahead of time:
attr
in CSS can only apply to content strings, not URLs or RGB colorsThis is not practical as i did not write this for real world uses, just to give you a example of what can be achieved.
this currently add's a / or appends a Style element which contains your necessary attribute's which will take affect on the target element's after Pseudo element.
this can be used as
and
as after: and before: pseudo elements are not directly accessible through DOM it is currently not possible to edit the Specific values of the css freely.
my way was just a example and its not good for practice, you can modify it try some of your own tricks and make it correct for real world usage.
so do your own experimentation's with this and others!
regards - Adarsh Hegde.
I made use of variables defined in
:root
insideCSS
to modify the:after
(the same applies to:before
) pseudo-element, in particular to change thebackground-color
value for a styledanchor
defined by.sliding-middle-out:hover:after
and thecontent
value for anotheranchor
(#reference
) in the following demo that generates random colors by using JavaScript/jQuery:HTML
CSS
JS/jQuery
You can't select pseudo elements in jQuery because they are not part of DOM. But you can add an specific class to the father element and control its pseudo elements in CSS.
EXAMPLE
In jQuery:
In CSS: