Property 'select' does not exist on type HTMLElement

avatar

Borislav Hadzhiev

Last updated: Mar 19, 2022

banner

Photo from Unsplash

Property 'select' does not exist on type HTMLElement #

The error "Property 'select' does not exist on type 'HTMLElement'" occurs when we try to call the select() method on an element that has a type of HTMLElement. To solve the error, use a type assertion to type the element as HTMLInputElement before calling select.

property select not exist type htmlelement

This is the index.html file for the examples in this article.

index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> </head> <body> <input type="text" id="text-box" size="20" value="Hello world!" /> <button id="btn">Select text</button> <script src="./src/index.ts"></script> </body> </html>

And here is an example of how the error occurs in the index.ts file.

src/index.ts
// 👇️ const input: HTMLElement | null const input = document.getElementById('text-box'); const btn = document.getElementById('btn'); btn?.addEventListener('click', () => { // ⛔️ Property 'select' does not exist on type 'HTMLElement'. Did you mean 'onselect'?ts(2551) input?.select(); });

The reason we got the error is that the return type of the document.getElementById method is HTMLElement | null and the select method doesn't exist on the HTMLElement type.

To solve the error, use a type assertion to type the element as an HTMLInputElement (or HTMLTextAreaElement if typing a textarea).

src/index.ts
const input = document.getElementById('text-box') as HTMLInputElement | null; const btn = document.getElementById('btn'); btn?.addEventListener('click', () => { input?.select(); });

Type assertions are used when we have information about the type of a value that TypeScript can't know about.

We effectively tell TypeScript that the input variable stores anHTMLInputElement or a null value and not to worry about it.

We used a union type to specify that the variable could still be null, because if an HTML element with the provided id does not exist in the DOM, the getElementById() method returns a null value.

We used the optional chaining (?.) operator to get around the possibly null value in the example.

src/index.ts
const input = document.getElementById('text-box') as HTMLInputElement | null; const btn = document.getElementById('btn'); // 👇️ optional chaining (?.) btn?.addEventListener('click', () => { // 👇️ optional chaining (?.) input?.select(); });

The optional chaining operator short-circuits returning undefined if the reference is equal to null or undefined.

In other words, if the input variable stores a null value, we won't attempt to call the select() method on null and get a runtime error.

Alternatively, you can use a simple if statement that serves as a type guard.

src/index.ts
const input = document.getElementById('text-box') as HTMLInputElement | null; const btn = document.getElementById('btn'); if (btn != null) { btn.addEventListener('click', () => { // 👉️ input has type HTMLInputElement or null here if (input != null) { // 👉️ input has type HTMLInputElement here input.select(); } }); }

We explicitly check that the input variable does not store a null value.

TypeScript knows that the input variable has a type of HTMLInputElement in the if block and allows us to directly call the select() method.

Which approach you pick to exclude null from the type before calling the select() method is a matter of personal preference.

However, it's always a best practice to include null in the type assertion, because the getElementById method would return null if no element with the provided id was found.

Conclusion #

The error "Property 'select' does not exist on type 'HTMLElement'" occurs when we try to call the select() method on an element that has a type of HTMLElement. To solve the error, use a type assertion to type the element as HTMLInputElement before calling select.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.