Ladies and gentlemen, there are a number of blocks, for example <p> . How do I get the sum of the height of the blocks? The method given below in the example of something does not work, I do not understand what the matter is ...

 var par = document.getElementsByTagName('p'); for (var i = 0; i < par.length; i++) { var parHeight = +par[i].getBoundingClientRect().height; } console.log(parHeight); 
 p { width: 50%; height: 100px; background: gray; margin: 20px; } 
 <p></p><p></p> 

    1 answer 1

    Your parHeight is equal to the last one, tk. you re-declare it every iteration.

     var par = document.getElementsByTagName('p'); var parHeight = 0; for (var i = 0; i < par.length; i++) { parHeight += par[i].getBoundingClientRect().height; } console.log(parHeight); 
     p { width: 50%; height: 100px; background: gray; margin: 20px; } 
     <p></p><p></p> 

    • There was a thought ... I don’t even know why I didn’t try this way ... Thank you ... - Air