Skip to content

Instantly share code, notes, and snippets.

@eikes
Created April 23, 2012 13:29
Show Gist options
  • Save eikes/2470949 to your computer and use it in GitHub Desktop.
Save eikes/2470949 to your computer and use it in GitHub Desktop.
Determine html element width without jquery
// Function to determine the width of any element in pixel:
getElementWidth = function (node) {
var ow = node.offsetWidth,
cs = node.currentStyle && node.currentStyle["width"]
|| getComputedStyle && getComputedStyle(node, null).getPropertyValue("width"),
result = parseFloat(ow || cs);
if (cs && cs.match(/%/))
return (parseFloat(cs) / 100) * getElementWidth(node.parentNode);
if (isNaN(result) || result == 0)
return getElementWidth(node.parentNode);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment