Daniel Williams
Daniel Williams

Reputation: 2317

Referencing a CSS class with javascript

<html>
<body>
<head>

<style type="text/css">
#someClass {
    color:red;
}
</style>

</head>

<div id="someClass"></div>

<script type="text/javascript">
alert(document.getElementById("someClass").style.color);
</script>

</body>
</html>

As you can see from my code I'm trying to figure out if I can reference a style attribute for a class that's defined in CSS, as opposed to directly in the tag's style attribute.

Upvotes: 0

Views: 103

Answers (1)

jdphenix
jdphenix

Reputation: 15425

You're looking for window.getComputedStyle() - small usage example here.

   alert(window.getComputedStyle(document.getElementById('someClass')).color); 
#someClass {
    color:red;
}
<div id="someClass"></div>

Upvotes: 2

Related Questions