Skip to content

Instantly share code, notes, and snippets.

@EdCharbeneau
Last active August 29, 2015 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdCharbeneau/f035747c48943be4337d to your computer and use it in GitHub Desktop.
Save EdCharbeneau/f035747c48943be4337d to your computer and use it in GitHub Desktop.
Get and display css properties at runtime.
(function () {
displayCssInfoFor();
})();
// A simple helper to display the CSS properties of an element at runtime.
// Use:
// By defualt, displays the color of the previous sibiling.
// <span data-properties-for></span>
// Specify element
// <span data-properties-for='{"element": "selector"}'></span>
// Specify properties
// <span data-properties-for='{"css": "[array of css properties]"}'></span>
// Specify both
// <span data-properties-for='{"element": "selector", "css": "[array of css properties]"}'></span>
function displayCssInfoFor() {
var selector = "[data-properties-for]";
var $elems = $(selector);
$elems.each(function (index, elem) {
var $displayElem = $(elem);
var $targetElem = $displayElem.data("properties-for").element || $(elem).prev();
var styles = $displayElem.data("properties-for").css || ["color"];
styles.forEach(function (style, i) {
var isLast = styles.length - 1 === i;
$displayElem.append(style + ": " + $($targetElem).css(style));
if (!isLast) $displayElem.append("; ")
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment