-2

What is the equivalent to the below code in pure javascript?

 $('.div1').height($('.div2').height());
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Coder
  • 3
  • 3
  • 6
  • 8
    What have you tried so far? (This isn't difficult if you do a little bit of a google search.) – evolutionxbox Feb 06 '17 at 13:00
  • Possible duplicate of [Get div height with plain JavaScript](http://stackoverflow.com/questions/15615552/get-div-height-with-plain-javascript) – Arkej Feb 06 '17 at 13:01
  • Possible duplicate of [How do you get the rendered height of an element?](https://stackoverflow.com/questions/526347/how-do-you-get-the-rendered-height-of-an-element) – Penny Liu Aug 06 '19 at 15:20

3 Answers3

8

That will be the following:

document.querySelector(".div1").style.height = window.getComputedStyle(document.querySelector(".div2")).height;
Technohacker
  • 735
  • 5
  • 19
0

Using document.getElementsByClassName

var height = document.getElementsByClassName("div1")[0].style.height;
document.getElementsByClassName("div2")[0].style.height = height;
Z-Bone
  • 1,534
  • 1
  • 10
  • 14
satchcoder
  • 797
  • 5
  • 11
0

Loop through all the elements with the class name, setting them all to the desired height. Like this:

var div1s = document.getElementsByClassName(".div1");
var height = document.getElementsByClassName(".div2")[0].height;
for (int i = 0; i < div1s.length; i++) {
     div1s[i].height = height;
}
Leonid
  • 708
  • 5
  • 12