Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How I select element that is a pseudo-element with JS? querySelector is not working.

<style>
div::before{content:'AAAAAA';}
</style>
<div></div>

<script>
 const before= document.querySelector('div::before')
alert(before)
</script>

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

It seems elements like that are handled via CSS specifically and not the DOM. But there's always tricks out there you can use. such as adding the styles you want to a class and targeting it with JavaScript, that way.

Here's something about that from StackOverflow.

You can use a different API called CSSOM. Personally it's not something I've used but it's an option https://stackoverflow.com/questions/29260296/modify-pseudo-select-after-in-javascript

or with jQuery. https://stackoverflow.com/questions/5041494/selecting-and-manipulating-css-pseudo-elements-such-as-before-and-after-usin