Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add :after in javascript

html:

This is what I have

<div class="one"> Apple  </div>

I want to add the word "juice" using javascript .style. Is there a equivalent to :: after where I can set the content in javascript using .style.after = "juice"?

<div class="one"> Apple Juice </div>

js:

document.querySelector('.one').style.display ="inline";
document.querySelector('.one').style.marginLeft ="2px";
document.querySelector('.one').style. = 
like image 766
Deke Avatar asked Sep 02 '26 11:09

Deke


1 Answers

I can think of a rather odd way of achieving what you want:

var style = document.createElement('style');
style.innerText = '.one::after { ... }';
document.body.appendChild(style)

So essentially just creating an entirely new <style></style> element and adding your pseudo selector styles there, and appending it to the body.

like image 75
Arnoux Avatar answered Sep 05 '26 01:09

Arnoux