Raiko L
Raiko L

Reputation: 1

How can I get ALL class properties in JavaScript

Is it somehow possible to get all properties of a class through JavaScript? Lets say I have a class

.menu { color: black; width: 10px; }

How can I get "color: black; width: 10px;" as a string through JavaScript?

Thank you!

Upvotes: 0

Views: 385

Answers (1)

repzero
repzero

Reputation: 8412

You can use getComputedStyle(). This will find all inline-style or css styling done in a css file..This returns all properties computed of the element

See snippet below

var el=document.getElementsByClassName("menu")[0];
style=getComputedStyle(el);
console.log(style);
.menu{
color:green;
background:blue;
opacity:1
}
<div class="menu"></div>

You can also get a specific property from the class in the above snippet, example

style.width

Upvotes: 3

Related Questions