Is it possible to set styles to pseudo elements in Javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Is it possible to set styles to pseudo elements in Javascript?

Kindly show your workaround code 😉

27th Apr 2017, 4:32 AM
Calviղ
Calviղ - avatar
10 Answers
+ 6
I doesn't seen 'in javascript' when I previously read your question ^^ You cannot set pseudo-style element by classical way of using 'style' attribute of html element, but you can: - set a classname to this element, with style definition for the targeted pseudo-element ( easiest ) - set dynamically style sheet rules ( hardiest - require browsers specificities: not handle by same way in ie, firefox, chrome... )
27th Apr 2017, 5:16 AM
visph
visph - avatar
+ 5
@visph. I need to check how to set style to pseudo elements in JAVASCRIPT.
27th Apr 2017, 5:12 AM
Calviղ
Calviղ - avatar
+ 5
@Very Hard: What a great sharing stuff ^^ : you post in comment instead answer here, and delete it quickly :P Anyway... I'm probably wrong, but using querySelector() method with pseudo-element selector seems not to work :( ( but I have no more time now to continue testing, and I cannot look again at your deleted comment )
27th Apr 2017, 5:38 AM
visph
visph - avatar
+ 4
Yes; to be sure we are sharing the same definition of "pseudo element" ;P ... and first link provide styling example ( no need of workaround, as you can ^^ )
27th Apr 2017, 4:55 AM
visph
visph - avatar
+ 4
document.querySelector('div::before') will return null with this in html: <style>div::before { content:'before'; }</style> <div>test</div>
27th Apr 2017, 5:41 AM
visph
visph - avatar
+ 3
Hi @visph. This is css reference.
27th Apr 2017, 4:47 AM
Calviղ
Calviղ - avatar
+ 3
@Very hard 😕
27th Apr 2017, 5:26 AM
Calviղ
Calviղ - avatar
+ 3
Thanks anyway @Very. I got it.
27th Apr 2017, 5:30 AM
Calviղ
Calviղ - avatar
+ 3
@visph you're right. Js cannot set the style directly. I have the other method to workaround this now by remove and add class
27th Apr 2017, 5:33 AM
Calviղ
Calviղ - avatar