简体   繁体   中英

Programmatically select text between two points in a textarea

I want to programmatically select a snippet of text in a textarea between two points, for example from character 50 to 58.

I've tried textarea.setSelectionRange(50, 58) but doesn't work

Assuming your text area to be:

<textarea id="myTextArea">Some initial text</textarea>

you need to set focus first.

let textarea = document.getElementById("myTextArea");
textarea.focus();
textarea.setSelectionRange(50, 58);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM