roostaamir roostaamir - 1 year ago 53
HTML Question

Is there a javascript library that reads a style string and adds the appropriate styles to given elements?

I am looking for a library/code that reads styles for the elements from a string and then applies the styles to the elements in that string. For example consider this code:


I want this library to read the string passed to it as parameter and then set the styles for
, and

does such a thing exist?

Answer Source

You don't need a library or any complicated parsing code to do this - you can simply insert a <style> element with the relevant text - the browser will parse and use it as normal.

var s = document.createElement('style');
s.innerText = "body{width:90%}div{background:red}.blue{color:blue}";
<div class="blue">xyz</div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download