Why not consider implementing the following function, extracted from this particular response:
function getStyles(a) {
var sheets = document.styleSheets, stylesObject = {};
for (var i in sheets) {
var rules = sheets[i].rules || sheets[i].cssRules;
for (var r in rules) {
if (a.is(rules[r].selectorText)) {
stylesObject = $.extend(stylesObject, css2json(rules[r].style), css2json(a.attr('style')));
}
}
}
return stylesObject;
}
function css2json(css) {
var styleObj = {};
if (!css) return styleObj;
if (css instanceof CSSStyleDeclaration) {
for (var i in css) {
if ((css[i]).toLowerCase) {
styleObj[(css[i]).toLowerCase()] = (css[css[i]]);
}
}
} else if (typeof css == "string") {
css = css.split("; ");
for (var i in css) {
var splitCss = css[i].split(": ");
styleObj[splitCss[0].toLowerCase()] = (splitCss[1]);
}
}
return styleObj;
}
Here's an example of how you can use it:
var elementStyles = getStyles($("#myDiv")); // Get all styles from $("#myDiv")
$("#elementToApplyStyle").css(elementStyles); // Apply those styles to another element.
Next, to access each element within the <div>
:
// .find('*') selects every child element.
$('#myDiv').find('*').each(function() {
// Perform actions on each element using `this`
console.log(this);
});
Utilize both methods together to gather the necessary information.