JSFiddle - React, Tailwind, and code Playground
by Mohammed Ismail Ansari
April 30, 2013
HTML
<div id="greatDiv" class="niceDiv">
This is the DIV
</div>
<button id="doIt">Do It!</button>
<button id="removeIt">Remove It!</button>
<div id="anotherDiv">
This is another Div
</div>
<hr />
<div id="consoleDiv">
</div>
CSS
#greatDiv
{
background-color: Yellow;
color: Red;
}
.niceDiv
{
height: 200px;
width: 200px;
}
JavaScript
function css(a) {
var sheets = document.styleSheets, o = {};
for(var i in sheets) {
var rules = sheets[i].rules || sheets[i].cssRules;
for(var r in rules) {
if(a.is(rules[r].selectorText)) {
o = $.extend(o, css2json(rules[r].style), css2json(a.attr('style')));
}
}
}
return o;
}
function css2json(css) {
var s = {};
if(!css) return s;
if(css instanceof CSSStyleDeclaration) {
for(var i in css) {
if((css[i]).toLowerCase) {
s[(css[i]).toLowerCase()] = (css[css[i]]);
}
}
} else if(typeof css == "string") {
css = css.split("; ");
for (var i in css) {
var l = css[i].split(": ");
s[l[0].toLowerCase()] = (l[1]);
};
}
return s;
}
$(document).ready(function(){
$("#doIt").click(function(){
var style = css($("#greatDiv"));
$("#anotherDiv").css(style);
});
$("#removeIt").click(function(){
$("#anotherDiv").removeAttr("style");
});
});
function writeLine(theText) {
$("#consoleDiv").append(theText + "<br />");
};