Selecting and manipulating CSS pseudo-elements such as ::before and ::after using javascript (or jQuery)
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
You cannot directly select ::before or ::after with JavaScript or jQuery because pseudo-elements are not real DOM nodes. They are generated boxes defined by CSS. The practical solution is to control them indirectly by changing classes, CSS custom properties, or stylesheet rules on the real element they belong to.
Why Direct Selection Does Not Work
This does not work because the pseudo-element is not part of the DOM tree:
There is no actual node for querySelector to return. jQuery has the same limitation. The parent element is selectable, but the pseudo-element exists only as a styling concept created during layout and painting.
The Best Default: Drive Pseudo-Elements With CSS Variables
A clean pattern is to define the pseudo-element in CSS using a custom property, then change that property from JavaScript.
This works well because the pseudo-element remains defined in CSS, while JavaScript only supplies dynamic values.
Toggle Classes to Switch Pseudo-Element States
If you need a small number of predefined visual states, class toggling is usually the simplest option.
This is often easier to maintain than injecting raw content strings from JavaScript.
Read Computed Styles When Needed
You cannot grab the pseudo-element as a node, but you can inspect its computed style.
This is useful for debugging or for tests that need to confirm which pseudo-element styles are active.
Modify Stylesheet Rules Carefully
For global behavior or theme-level changes, you can edit a stylesheet rule directly. This is more advanced and usually less convenient than classes or CSS variables.
This works, but it is usually not the first choice because it couples script logic tightly to stylesheet structure and ordering.
jQuery Does Not Change the Limitation
jQuery can still help you manipulate the real element:
But jQuery does not make pseudo-elements selectable. It only makes the indirect techniques more concise.
Think About Accessibility and Semantics
Pseudo-elements are great for decorative labels, arrows, icons, and badges, but they are a poor place for information that must be reliably read by assistive technology or copied by users. If the content is semantically important, put it in the real HTML and style it there. A useful rule is: if removing CSS would remove essential meaning, that text probably should not live only in ::before or ::after.
Common Pitfalls
A common mistake is trying to attach events to ::before or ::after as though they were DOM elements. Event handling still belongs to the real parent element.
Another mistake is putting too much dynamic content into pseudo-elements. They are excellent for decoration and small labels, but if the content is semantically important, it often belongs in the real HTML.
It is also easy to forget that the CSS content property needs properly quoted strings when set through custom properties or inserted rules.
Summary
- '
::beforeand::aftercannot be selected directly because they are not DOM nodes.' - Control pseudo-elements indirectly through classes, CSS variables, or stylesheet rules.
- Use
getComputedStyle(element, "::before")to inspect active pseudo-element styles. - jQuery can help manipulate the parent element, but it cannot bypass the DOM limitation.
- Prefer real HTML when the content is semantic rather than decorative.

