Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
The setRangeText() method of the HTMLTextAreaElement interface replaces a range of text in a <textarea> element with new text passed as the argument.
Additional optional parameters include the start of the section of text to change, the end of the section, and a keyword defining what part of the <textarea> should be selected after the text is updated. If the startSelection and endSelection arguments are not provided, the range is assumed to be the selection.
The final argument determines how the selection will be set after the text has been replaced. The possible values are "select", which selects the newly inserted text, "start", which moves the selection to just before the inserted text, "end", which moves the selection to just after the inserted text, or the default, "preserve", which tries to preserve the selection.
In addition, the select and selectionchange events are fired.
The string to insert.
selectionStart OptionalThe index of the first selected character. An index greater than the length of the element's value is treated as pointing to the end of the value.
selectionEnd OptionalThe index of the character after the last selected character. An index greater than the length of the element's value is treated as pointing to the end of the value. If selectionEnd is less than selectionStart, then both are treated as the value of selectionEnd.
selectMode OptionalA keyword, either select, start, end, or the default preserve, defining how the selection should be set after the text has been replaced.
None (undefined).
Click the button in this example to replace part of the text in the text box. The newly inserted text will be highlighted (selected) afterwards.
| HTML # dom-textarea/input-setrangetext-dev |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 30, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.