|
| 1 | +--- |
| 2 | +title: "HTMLTextAreaElement: setRangeText() method" |
| 3 | +short-title: setRangeText() |
| 4 | +slug: Web/API/HTMLTextAreaElement/setRangeText |
| 5 | +page-type: web-api-instance-method |
| 6 | +browser-compat: api.HTMLTextAreaElement.setRangeText |
| 7 | +--- |
| 8 | + |
| 9 | +{{APIRef("HTML DOM")}} |
| 10 | + |
| 11 | +The **`setRangeText()`** method of the {{domxref("HTMLTextAreaElement")}} interface replaces a |
| 12 | +range of text in an {{HTMLElement("textarea")}} element with new text passed as the argument. |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +In addition, the {{domxref("HTMLTextAreaElement.select_event", "select")}} and {{domxref("HTMLTextAreaElement.selectionchange_event", "selectchange")}} events are fired. |
| 19 | + |
| 20 | +## Syntax |
| 21 | + |
| 22 | +```js-nolint |
| 23 | +setRangeText(replacement) |
| 24 | +setRangeText(replacement, startSelection) |
| 25 | +setRangeText(replacement, startSelection, endSelection) |
| 26 | +setRangeText(replacement, startSelection, endSelection, selectMode) |
| 27 | +``` |
| 28 | + |
| 29 | +### Parameters |
| 30 | + |
| 31 | +- `replacement` |
| 32 | + - : The string to insert. |
| 33 | +- {{domxref("HTMLTextAreaElement.selectionStart", "selectionStart")}} {{optional_inline}} |
| 34 | + - : The index of the first selected character. An index greater than the length |
| 35 | + of the element's value is treated as pointing to the end of the value. |
| 36 | +- {{domxref("HTMLTextAreaElement.selectionEnd", "selectionEnd")}} {{optional_inline}} |
| 37 | + - : The index of the character _after_ the last selected character. An |
| 38 | + index greater than the length of the element's value is treated as pointing to the end |
| 39 | + of the value. If `selectionEnd` is less than `selectionStart`, then both are treated as the value of `selectionEnd`. |
| 40 | +- `selectMode` {{optional_inline}} |
| 41 | + - : A keyword, either `select`, `start`, `end`, or the default `preserve`, defining how the selection should be set after the text has been replaced. |
| 42 | + |
| 43 | +### Return value |
| 44 | + |
| 45 | +None ({{jsxref("undefined")}}). |
| 46 | + |
| 47 | +## Examples |
| 48 | + |
| 49 | +Click the button in this example to replace part of the text in the text box. The newly |
| 50 | +inserted text will be highlighted (selected) afterwards. |
| 51 | + |
| 52 | +### HTML |
| 53 | + |
| 54 | +```html |
| 55 | +<label for="ta">Example text input:</label> |
| 56 | +<textarea id="ta"> |
| 57 | + This text has NOT been updated. |
| 58 | +</textarea> |
| 59 | +<button id="btn">Update text</button> |
| 60 | +``` |
| 61 | + |
| 62 | +### JavaScript |
| 63 | + |
| 64 | +```js |
| 65 | +const btn = document.getElementById("btn"); |
| 66 | + |
| 67 | +btn.addEventListener("click", () => { |
| 68 | + changeText(); |
| 69 | +}); |
| 70 | + |
| 71 | +function changeText() { |
| 72 | + const textarea = document.getElementById("text-box"); |
| 73 | + textarea.focus(); |
| 74 | + textarea.setRangeText("ALREADY", 14, 17, "select"); |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### Result |
| 79 | + |
| 80 | +{{EmbedLiveSample("Examples")}} |
| 81 | + |
| 82 | +## Specifications |
| 83 | + |
| 84 | +{{Specifications}} |
| 85 | + |
| 86 | +## Browser compatibility |
| 87 | + |
| 88 | +{{Compat}} |
| 89 | + |
| 90 | +## See also |
| 91 | + |
| 92 | +- {{HTMLElement("textarea")}} |
| 93 | +- {{domxref("HTMLTextAreaElement")}} |
| 94 | +- {{domxref("HTMLTextAreaElement.select()")}} |
| 95 | +- {{domxref("HTMLTextAreaElement.setSelectionRange()")}} |
| 96 | +- {{domxref("HTMLTextAreaElement.textLength")}} |
| 97 | +- {{domxref("Selection")}} |
| 98 | +- {{cssxref("::selection")}} pseudo-element |
0 commit comments