Last active
December 31, 2015 18:09
-
-
Save shaunwallace/8025178 to your computer and use it in GitHub Desktop.
Computing the total width of all children elements inside of a given parent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//with jQuery | |
var parent = $(selector) | |
, totalWidthOfChildren = Array.prototype.reduce.call(parent.children(), function(a,b){ return a + $(b).outerWidth( true ); }, 0); | |
//without jquery | |
var parent = document.getElementById('selector') | |
, totalWidthOfChildren = Array.prototype.reduce.call(parent.children, function(a,b){ return a + outerWidth(b); }, 0); | |
function outerWidth( elem ) { | |
var styles; | |
if( window.getComputedStyle ) { | |
var parts = ["width", "paddingLeft", "paddingRight", "marginLeft", "marginRight"]; | |
styles = window.getComputedStyle( elem ); | |
return parts.reduce(function(a,b) { | |
return a + +styles[b].replace(/px/, ''); | |
}, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment