How to Get Content of ::after Pseudo-Element Using JavaScript?

Find out how to get content value of ::after pseudo-element in JavaScript

In JavaScript, to get the content property value, as specified by ::after pseudo-element, you can use the window.getComputedStyle() method. It exposes the CSSStyleDeclaration object (as read-only), from which you can access the style information of ::after pseudo-element.

For example:

<div>
    <a id="foo" href="#">foobar</a>
</div>
a::after { content: ' [' attr(href) ']'; }
const element = document.getElementById('foo');
const styles = window.getComputedStyle(element, '::after');
const content = styles.content;

console.log(content); // ' [#]'

If the content property is not defined, then its computed value "none" is returned.


Hope you found this post useful. It was published . Please show your love and support by sharing this post.