We\'d like to convert a CSS style entered as string into a JS object.
E.g.,
var input = \" border:solid 1px; color:red \";
expec
Just for fun and for completeness…
I haven't checked cross-browser compatibility (only tried in Chrome), and it has some quirks:
var input = "font-weight:bold; color: blue; margin: 0 15px";
var e = document.createElement("div");
e.setAttribute("style", input);
var output = {};
for (var i = 0; i < e.style.length; i++) {
var name = e.style[i];
var value = e.style.getPropertyValue(name);
output[name] = value;
}
The quirk is that even though we passed in a single margin
declaration, we get an object like
{
color: "blue",
font-weight: "bold",
margin-bottom: "0px",
margin-left: "15px",
margin-right: "15px",
margin-top: "0px",
}
This might be a good or a bad thing depending on what you're after.