4

I am trying to get the width of an element using JS in my Angular app.

document.getElementsByClassName("element")[0].offsetWidth;

When I do this i am getting the below compilation error

Property 'of fsetWidth' does not exist on type 'Element'.

Could someone tell me how to use it in Angular 4. Thanks

1 Answers1

5

If give identification of your DOM element using #

Template:

<div class="for-example" #titleText>For Example</div>

Component:

Declare

@ViewChild('titleText') titleTextElement;

and get offsetWidth like following:

this.titleTextElement.nativeElement.offsetWidth
Wasif Khan
  • 956
  • 7
  • 25