function copy(){
var Url=document.getElementById("Id");
Url.select(); //error
document.execCommand("Copy"); // browser copy
}
as above. I'm trying to make a function to copy text in browser.but the error as title occurred in typescript. the select() is valid I think(link),since I can copy correctly when I use it in a demo. my ts version is 2.8.1
The HTMLInputElement interface provides special properties and methods for manipulating the options, layout, and presentation of <input> elements.
You need to add a type assertion:
var Url = document.getElementById("Id") as HTMLInputElement;
Url.select(); // OK
getElementById
can return any HTMLElement
s. In your case you know its an input element so you can tell TypeScript that by using a type assertion 🌹.
select
method is defined for HTMLInputElements. The following will get rid of the TypeScript error.
let Url: HTMLInputElement = document.getElementById("Id") as HTMLInputElement;
Url.select();
document.execCommand("Copy");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With