+ 24

[Solved] Can we also select after and before pesudo of an element by JavaScript ?

I have a question how can we select element::after and element::before pesudo by a JavaScript! HTML: <div class="element"></div> CSS : .element{ //your code} .element::after{ //your code} .element::before{ //your code} JS: //How can I select the pseudo element and give style to them ? //Also I know about jQuery it can select easily , but I want to know this in JavaScript!

12th Jun 2020, 10:09 AM
DIVYANSHU 😸
DIVYANSHU 😸 - avatar
7 ответов
+ 10
So easy bro There is the one way Use getComputedStyle() function bro https://code.sololearn.com/WdXBa21hv548/?ref=app Second thing you can use CSS variable and then change their values with javascript DIVYANSHU KR 😺 hope this help you bro 😃
12th Jun 2020, 11:17 AM
Coder
Coder - avatar
+ 9
[Edited] Let see , I think JS experienced expert can answer this😄😄 Mikhail bro Coder has answer it!!
12th Jun 2020, 10:18 AM
DIVYANSHU 😸
DIVYANSHU 😸 - avatar
+ 7
I think there is no way to do that with JS..🤷‍♂️
12th Jun 2020, 10:13 AM
Mikhail Solovyev
Mikhail Solovyev - avatar
+ 7
DIVYANSHU KR 😺 Yeah, if it happens, it will be very intetesting to learn!! So, thank you for posting this question👍👍👍
12th Jun 2020, 10:20 AM
Mikhail Solovyev
Mikhail Solovyev - avatar
+ 5
Coder i understand now how we can read the value of any property of pseudo-element using getComputedStyle()👍thank you!! But how can we manipulate these properties’ values? For example, i want to change pseudo-element’s value ‘red’ of property ‘color’ for ‘green’, what should i do?🤷‍♂️
18th Jun 2020, 6:51 AM
Mikhail Solovyev
Mikhail Solovyev - avatar
+ 5
Mikhail you can set the property of Pseudo element by JavaScript becoz they not in DOM elements the best way to do it is to use CSS variable Use CSS varible then change it through JavaScript Hope this help you bro 😃 I tried to change it bro I got no modification error
18th Jun 2020, 7:08 AM
Coder
Coder - avatar
+ 5
Coder i need to learn css variables for this as well👍 i don’t know them yet🤷‍♂️ ok thank you! P.s. if i don’t understand it, i’ll ask again for your help😅🙏
18th Jun 2020, 7:11 AM
Mikhail Solovyev
Mikhail Solovyev - avatar