0

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

I need to get this with vanilla js.

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

DooD
  • 33
  • 1
  • 4

1 Answers1

0

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);