2

I am using angular 5 where in a component I have one method

onFullScreen : function (event){ console.log(event[0]) }

Here, when I do console.log(event[0]), this will return this

enter image description here

This returns a HTMLDivElement, now I want to get the height property in my onFullScreen() method. How to get it?

Yash Jain
  • 752
  • 1
  • 12
  • 34

2 Answers2

3

The best way to get the height of an element (and lots of other layout-related properties) is to use getBoundingClientRect (https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect).

So you can do this:

const height = event[0].getBoundingClientRect().height;
Richard
  • 992
  • 1
  • 11
  • 27
Oscar Paz
  • 18,084
  • 3
  • 27
  • 42
1

You can try this:

var height = document.getElementById('element').style.height;

Since the height is defined in inline styles for this element, this would work.

Warning: this does not work if height is not explicitly defined in css but calculated based on content or outer box.

Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
sree kumar
  • 44
  • 7
  • there's nothing like rectangle here, I made rectangle in image so that it will be easy to recognize where I am getting the HTMLDivElement and height property. – Yash Jain Apr 09 '18 at 06:46
  • 3
    This wouldn't work. `style.height` only would return the `height` property set in the `style` attribute. It may return pixels, a percentage, or, probably, nothing. – Oscar Paz Apr 09 '18 at 06:54