Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getComputedStyle from the `before` pseudo element

Tags:

javascript

css

I am trying to get the computed style from a :before selector of an element.

I have tried this, but it's not working, how can I make this work?

var a = window.getComputedStyle(document.querySelector('#one:before'), null);
alert(a.getPropertyValue("content"))

https://jsfiddle.net/99qe4knh/5/

like image 352
gespinha Avatar asked Mar 12 '15 17:03

gespinha


1 Answers

According to MDN, the second parameter to the .getComputedStyle() method is the pseudo element:

var style = window.getComputedStyle(element[, pseudoElt]);

pseudoElt (Optional) - A string specifying the pseudo-element to match. Must be omitted (or null) for regular elements.

Therefore you should use the following:

var a = window.getComputedStyle(document.querySelector('#one'), ':before');

alert(a.getPropertyValue("content"));

Updated Example

like image 64
Josh Crozier Avatar answered Sep 21 '22 01:09

Josh Crozier