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 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. One more doubt. How can I access child component reference variable in parent component
Check this out for your doubt. stackoverflow.com/questions/36018478/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.