Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select text in TypeScript [duplicate]

how can I select text in input field in TypeScript.

in Javascript this wpuld look like this:

document.getElementById('id').select();

but TypeScript does not support select (error: [ts] Property 'select' does not exist on type 'HTMLElement'.)

Added a few think: It may be a problem beacouse I use dx controls and they create a wraper. You can see here in html:

<dx-text-box _ngcontent-c1="" id="test" ng-reflect-disabled="false" ng-reflect-value="0:00" class="dx-texteditor dx-widget dx-textbox">
<div class="dx-texteditor-container">
<input autocomplete="off" class="dx-texteditor-input" type="text" spellcheck="false" tabindex="0" role="textbox">
<div data-dx_placeholder="" class="dx-placeholder dx-state-invisible"></div><div class="dx-texteditor-buttons-container">
</div></div></dx-text-box>
like image 740
Jan Avatar asked Dec 10 '22 09:12

Jan


1 Answers

document.getElementById(string) returns an HTMLElement which does not define a select() method. HTMLInputElement DOES define a select() method and also extends HTMLElement, so if you cast as HTMLInputElement, it should work:

 (document.getElementById('id') as HTMLInputElement).select();
like image 150
Brian Ball Avatar answered Dec 20 '22 20:12

Brian Ball