Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property 'style' does not exist on type 'Element' #16920

Closed
itayod opened this issue Jul 4, 2017 · 10 comments
Closed

Property 'style' does not exist on type 'Element' #16920

itayod opened this issue Jul 4, 2017 · 10 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@itayod
Copy link

itayod commented Jul 4, 2017

TypeScript Version: ~2.3.3

Code

  let floorElements = document.getElementsByClassName("floor");
    for(var i in floorElements) {
      floorElements[i].style.height = AppConstants.FLOOR_SIZE_IN_PX + 'px';
    }

Expected behavior:
Not display an error.
Actual behavior:
I get this error : Property 'style' does not exist on type 'Element'

@ThomasdenH
Copy link
Contributor

ThomasdenH commented Jul 4, 2017

You need to cast the Element to a HTMLElement, which has got the style property. The reason is that it could also be another type of element that hasn't got the style property.

@kitsonk
Copy link
Contributor

kitsonk commented Jul 4, 2017

The simplest way is to cast the return of getElementsByClassName:

let floorElements = document.getElementsByClassName("floor") as HTMLCollectionOf<HTMLElement>;

Again though, these sorts of general questions should be being asked in Gitter or on StackOverflow.

@itayod
Copy link
Author

itayod commented Jul 5, 2017 via email

@rbuckton rbuckton closed this as completed Aug 5, 2017
@rbuckton rbuckton added the Working as Intended The behavior described is the intended behavior; this is not a bug label Aug 5, 2017
@Rep1ay
Copy link

Rep1ay commented Jan 4, 2018

@kitsonk it dosn't work with css style...I getting error "Property 'style' does not exist on type 'HTMLCollectionOf'."

@ThomasdenH
Copy link
Contributor

The function returns a collection of elements, so to access their style you should loop over the elements or index them directly.

@chadupton
Copy link

@Rep1ay

If you only have one element with that classname, try this:

let drawingBuffer:HTMLElement = document.getElementsByClassName("drawingBuffer")[0] as HTMLElement;

@CR7LOVE
Copy link

CR7LOVE commented Jun 27, 2018

@chadupton It works.Thank you!

@MrBugatti
Copy link

MrBugatti commented Jul 5, 2018

Syntactically best way for a single element (below). You don't need to identity the first element within array if it's the only element.

const node = document.querySelector(element) as HTMLElement;

@Umaid1
Copy link

Umaid1 commented Apr 8, 2019

@Rep1ay

If you only have one element with that classname, try this:

let drawingBuffer:HTMLElement = document.getElementsByClassName("drawingBuffer")[0] as HTMLElement;

Thanks it worked!

teddywing added a commit to teddywing/muttagen that referenced this issue Jun 10, 2019
Map sidebar toggle to `\m`.

Initialise the code after a timeout to account for Gmail's initial
loading screen.

Needed to use an `as HTMLElement` cast in order to get the code to
compile:
microsoft/TypeScript#16920

    src/sidebar.ts:10:5 - error TS2740: Type 'Element' is missing the
    following properties from type 'HTMLElement': accessKey,
    accessKeyLabel, autocapitalize, dir, and 110 more.
krishnakantsalkar added a commit to krishnakantsalkar/My-App that referenced this issue Sep 1, 2020
*typescript doesnt directly allow changing style of htmlelements
taken from classname

*import as unknown and convert to htmlcollectionofelements

*microsoft/TypeScript#16920

Signed-off-by: krishnakantsalkar <dev@krishnakantsalkar.gq>
krishnakantsalkar added a commit to krishnakantsalkar/My-App that referenced this issue Sep 5, 2020
*typescript doesnt directly allow changing style of htmlelements
taken from classname

*import as unknown and convert to htmlcollectionofelements

*microsoft/TypeScript#16920

Signed-off-by: krishnakantsalkar <dev@krishnakantsalkar.gq>
@titusfx
Copy link

titusfx commented Aug 11, 2021

At current date you can do

const node = document.querySelector<HTMLElement>(element);

Which is cleaner than use as operator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

10 participants