JavaScript: how to read the CSS content property

JavaScript: how to read the CSS content property

Through the DOM APIs we can access the CSS generated content. We can simply read the value of the CSS content property with JavaScript.

Through the DOM APIs we can access the CSS generated content. We can simply read the value of the CSS content property with JavaScript.

Given the following CSS code:


#test:before {
    content: 'Before ';
}

we can use the getComputedStyle() method as follows:


'use strict';

const getCSSContentValue = (element, where = ':before') => {
    return getComputedStyle(element, where).content;
};