I need to change the content attribute of the after and before pseudo elements of the next HTML element without using libraries like jQuery . If I did something similar with CSS it would be something like this:
#text::before{background:red;display:block,content:"HolaMundo";}
#text::after{background:blue;color:red,font-size:100px;}
What would be the way to do it with Javascript?
var text = document.getElementById("text");
text...
You can set ::after and ::before to load the content of an attribute by default.
In the example I used the "title" attribute:
And then update it with Javascript.
either
Unfortunately, it is not possible to select or change a psuedo element directly with JavaScript. However, it can be done with a combination of CSS and JavaScript. For example, you could create some classes with different content in the psuedo element, and use JavaScript to change the class:
CSS:
JavaScript:
As they said, you can't change the pseudo elements since they don't belong to the DOM.
In the following code I simulate with the help of attribute selectors and a little bit of
js
how it could be done:One thing you can do is create an element
<style id="myStyle">
into which you can add content mid-run and thus have a little more control over the situation. I have used it and it works great.