大网数据广告活动
Web 标准与前端开发

HTMLTextAreaElement: setRangeText() method

· 52CE 我爱测

The **`setRangeText()`** method of the HTMLTextAreaElement interface replaces a range of text in a textarea element with new text passed as the argument.

HTML DOM

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 HTMLTextAreaElement.select_event and HTMLTextAreaElement.selectionchange_event events are fired.

Syntax

setRangeText(replacement)
setRangeText(replacement, startSelection)
setRangeText(replacement, startSelection, endSelection)
setRangeText(replacement, startSelection, endSelection, selectMode)

Parameters

  • replacement
    • : The string to insert.
  • HTMLTextAreaElement.selectionStart
    • : The 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.
  • HTMLTextAreaElement.selectionEnd
    • : The 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
    • : A keyword, either select, start, end, or the default preserve, defining how the selection should be set after the text has been replaced.

Return value

None (undefined).

Examples

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

<label for="ta">Example text input:</label>
<textarea id="ta">
  This text has NOT been updated.
</textarea>
<button id="btn">Update text</button>

JavaScript

const btn = document.getElementById("btn");

btn.addEventListener("click", () => {
  changeText();
});

function changeText() {
  const textarea = document.getElementById("ta");
  textarea.focus();
  textarea.setRangeText("ALREADY", 14, 17, "select");
}

Result

Examples

Specifications

Browser compatibility

See also

  • textarea
  • HTMLTextAreaElement
  • HTMLTextAreaElement.select()
  • HTMLTextAreaElement.setSelectionRange()
  • HTMLTextAreaElement.textLength
  • Selection
  • ::selection pseudo-element

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。