The following recursive function is running on all HTML elements with tag names, starting from body element and getting down to the leafs. for each element, the code gets the background color and the element area in pixels (if exist):
function findAllElements(element){
if(element.tagName){
// getting the element background color and puts it in var "BGcolor"
// getting the element area and puts it in var "AREA"
// console.log both BGcolor and AREA with element.nodeName
var children =element.childNodes;
for (var i= 0;i<children.length;i++){
findAllElements(children[i]);
}
}
// do something
}
The problem:
I need to calculate for each element the relative area. for instance, if I have DIV inside DIV the inner DIV will be his area proper while the outer div will be his area minus the inner div area:
In the above picture the output should be: the inner DIV area is 100px and the outer DIV is 400-100 = 300px.
this can also be found in other elements (such as: STRONG tag inside P tag, TABLE inside DIV etc..)
What I need: I need to add to this specific recursive function a few code lines which will give me the relative area as seen above. one solution can be variant which sum's all children areas and reducing it from the parent node.
I didn't succeed adding something that will work... :(
important notes:
1) I'm running over big number of different elements so this should work for all types and not just for this DIV's example
2)I'm not looking for definitions how to take the color or how calculate the area, I already have it and in order to simplify the problem I didn't mention it. What I'm looking for is WHERE and HOW to add this child area
Thank you very much