I'm trying to find the widths of set of HTML elements on a web page.
Basically, I want to get the widths of:
- The
html
element - The
body
element - All elements directly under the
body
element [ancestor] - All children directly under 'ancestor' down to the fourth generation (if at all it goes that far down)
So looking at the HTML code below:
<html>
<head>
<title></title>
</head>
<body>
<div>
<div>
<div>
<div>
<div>Test 1</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>Test 2
<div>Test 2 - Child</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
I can get the widths of html
, body
, and the 'ancestors' [(1), (2), and (3)] pretty easily with:
console.log($('html').outerWidth());
console.log($('body').outerWidth());
$('body').children().each(function(){
console.log(this.outerWidth());
});
However, I'm not sure how to get the descendants of the 'ancestor' down to the 4th generation. So 4th generation would be where you have Test 1
and Test 2
, Test 2 - Child
would be fifth generation.
FYI I'm doing this on pages that I cannot control so identifiers like id
and class
will not be available for me to target. Also the descendants may vary tag-wise, they might not always be div
s.