0

i have a div, div1 with a height according to the text, div2 is the same. Now i need div3 to be: div1 + div2

I have this code, but it ain't workin.

var lengthnews1 = ((document.getElementById("div1").offsetHeight)+"px");
var lengthnews2 = ((document.getElementById("div2").offsetHeight)+"px");
var x = document.getElementById("div3");
x.height = (lengthnews1 + lengthnews2); 
peirix
  • 36,512
  • 23
  • 96
  • 126
Henk Jansen
  • 1,142
  • 8
  • 27

3 Answers3

3

you are trying to add two strings, not numbers. Fetch both heights, add them and then add the "px" :

var lengthnews1 = ((document.getElementById("div1").offsetHeight));
var lengthnews2 = ((document.getElementById("div2").offsetHeight));

var x = document.getElementById("div3");
x.height = (lengthnews1 + lengthnews2) + "px"; 

You can verify the working code at this fiddle

vvohra87
  • 5,594
  • 4
  • 22
  • 34
0

You need to remove the "px" from lengthnews1 and lengthnews2 variables so you can add the two values together for var x, once added you can then add the "px".

DevTez
  • 198
  • 1
  • 7
0

You need to set the height using x.style.height Also you need to concat with "px" only once, ie (lengthnews1 + lengthnews2) + "px"

Jad
  • 922
  • 8
  • 14