Aside from looping through an array that has each style property declared, is there any way to get a key/value output of all styling of a dom element?
My fallback is iterating through the keys listed on: http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSview-getComputedStyle
getComputedStyle() The Window. getComputedStyle() method returns an object containing the values of all CSS properties of an element, after applying active stylesheets and resolving any basic computation those values may contain.
The getComputedStyle() method is used to get all the computed CSS property and values of the specified element. The use of computed style is displaying the element after stylings from multiple sources have been applied. The getComputedStyle() method returns a CSSStyleDeclaration object.
The CSSStyleDeclaration interface represents an object that is a CSS declaration block, and exposes style information and various style-related methods and properties. A CSSStyleDeclaration object can be exposed using three different APIs: Via HTMLElement.
The computed style is the style used on the element after all styling sources have been applied. Style sources: external and internal style sheets, inherited styles, and browser default styles.
is there any way to get a key/value output of all styling of a dom element?
Yes, but don't expect the exact handling of values (units etc.) to be the same cross-browser.
var styles= [];
// The DOM Level 2 CSS way
//
if ('getComputedStyle' in window) {
var cs= getComputedStyle(element, '');
if (cs.length!==0)
for (var i= 0; i<cs.length; i++)
styles.push([cs.item(i), cs.getPropertyValue(cs.item(i))]);
// Opera workaround. Opera doesn't support `item`/`length`
// on CSSStyleDeclaration.
//
else
for (var k in cs)
if (cs.hasOwnProperty(k))
styles.push([k, cs[k]]);
// The IE way
//
} else if ('currentStyle' in element) {
var cs= element.currentStyle;
for (var k in cs)
styles.push([k, cs[k]]);
}
For those who come after me with the same question, the manual way works like this:
// Now, here's a list of styles we want to check:
var allStyles = ["azimuth","background","backgroundAttachment","backgroundColor","backgroundImage","backgroundPosition","backgroundRepeat","border","borderBottom","borderBottomColor","borderBottomStyle","borderBottomWidth","borderCollapse","borderColor","borderLeft","borderLeftColor","borderLeftStyle","borderLeftWidth","borderRight","borderRightColor","borderRightStyle","borderRightWidth","borderSpacing","borderStyle","borderTop","borderTopColor","borderTopStyle","borderTopWidth","borderWidth","bottom","captionSide","clear","clip","color","content","counterIncrement","counterReset","cssFloat","cue","cueAfter","cueBefore","cursor","direction","display","elevation","emptyCells","font","fontFamily","fontSize","fontSizeAdjust","fontStretch","fontStyle","fontVariant","fontWeight","height","left","letterSpacing","lineHeight","listStyle","listStyleImage","listStylePosition","listStyleType","margin","marginBottom","marginLeft","marginRight","marginTop","markerOffset","marks","maxHeight","maxWidth","minHeight","minWidth","orphans","outline","outlineColor","outlineStyle","outlineWidth","overflow","padding","paddingBottom","paddingLeft","paddingRight","paddingTop","page","pageBreakAfter","pageBreakBefore","pageBreakInside","pause","pauseAfter","pauseBefore","pitch","pitchRange","playDuring","position","quotes","richness","right","size","speak","speakHeader","speakNumeral","speakPunctuation","speechRate","stress","tableLayout","textAlign","textDecoration","textIndent","textShadow","textTransform","top","unicodeBidi","verticalAlign","visibility","voiceFamily","volume","whiteSpace","widows","width","wordSpacing","zIndex"];
// var allStyles = ["float"];
var setStyles = [];
// Now we loop through each property, and report those defined
$.each(allStyles, function(key, value){
if ($this.css(value) !== undefined){
setStyles.push(value, $this.css(value))
}
});
alert(setStyles.join(" <br /> "));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With