JavaScript – Get style attribute of HTML elements as string

The below code shows you how to get style attribute of HTML elements as string in JavaScript.

document.getElementById('message').style.cssText;

Let’s consider this example:

example.html
<div id="message" style="color: blue; padding: 10px;">
   Lorem Ipsum is simply dummy...
</div>

<script type="text/javascript">
    var styleAsString = document.getElementById('message').style.cssText;
    console.log(styleAsString); // color: blue; padding: 10px;
</script>

0 0 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x