Skip to content

Instantly share code, notes, and snippets.

@shawndumas
Created December 2, 2011 18:04
Show Gist options
  • Save shawndumas/1424210 to your computer and use it in GitHub Desktop.
Save shawndumas/1424210 to your computer and use it in GitHub Desktop.
Set Element's Style
//setStyle(
// document.getElementById('divId'),
// 'height: 180px; text-align: left; overflow-y: scroll;'
//);
var setStyle = function (elm, style) {
var rules = (function (style) {
var css, v,
rules = {},
div = document.createElement('div');
div.innerHTML = '<div style="' + style + '"></div>';
css = div.childNodes[0].style;
for (var i in css) {
(
i !== 'cssText' &&
(typeof i === 'string' || i instanceof String)
) &&
(v = css[i]) &&
(rules[i] = v);
}
return rules;
})(style);
for (var rule in rules) if (rules.hasOwnProperty(rule)) {
elm.style[rule] = rules[rule];
}
return elm;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment