| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setRangeText | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since January 2020.
The HTMLInputElement.setRangeText() method replaces a
range of text in an <input> or <textarea> element with
a new string.
setRangeText(replacement)
setRangeText(replacement, start)
setRangeText(replacement, start, end)
setRangeText(replacement, start, end, selectMode)
replacementThe string to insert.
start OptionalThe 0-based index of the first character to replace. Defaults to the current
selectionStart value (the start of the user's current selection).
end OptionalThe 0-based index of the character after the last character to replace.
Defaults to the current selectionEnd value (the end of the user's current
selection).
selectMode OptionalA string defining how the selection should be set after the text has been replaced. Possible values:
"select" selects the newly inserted text."start" moves the selection to just before the inserted text."end" moves the selection to just after the inserted text."preserve" attempts to preserve the selection. This is the default.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.
<input
type="text"
id="text-box"
size="30"
value="This text has NOT been updated." />
<button>Update text</button>
function selectText() {
const input = document.getElementById("text-box");
input.focus();
input.setRangeText("ALREADY", 14, 17, "select");
}
document.querySelector("button").addEventListener("click", selectText);
| Specification |
|---|
| HTML # dom-textarea/input-setrangetext-dev |
This page was last modified on Jun 23, 2025 by MDN contributors.
HTMLInputElementacceptalphaaltautocompletecapturecheckedcolorSpacedefaultCheckeddefaultValuedirNamedisabledfilesformformActionformEnctypeformMethodformNoValidateformTargetheightindeterminatelabelslistmaxmaxLengthminminLengthmultiplenamepatternplaceholderpopoverTargetActionpopoverTargetElementreadOnlyrequiredselectionDirectionselectionEndselectionStartsizesrcsteptypevalidationMessagevalidityvaluevalueAsDatevalueAsNumberwebkitdirectorywebkitEntrieswidthwillValidateBeforeUnloadEventDOMStringMapErrorEventHTMLAnchorElementHTMLAreaElementHTMLAudioElementHTMLBRElementHTMLBaseElementHTMLBodyElementHTMLButtonElementHTMLCanvasElementHTMLDListElementHTMLDataElementHTMLDataListElementHTMLDialogElementHTMLDivElementHTMLDocumentHTMLElementHTMLEmbedElementHTMLFieldSetElementHTMLFormControlsCollectionHTMLFormElementHTMLFrameSetElementHTMLGeolocationElementHTMLHRElementHTMLHeadElementHTMLHeadingElementHTMLHtmlElementHTMLIFrameElementHTMLImageElementHTMLLIElementHTMLLabelElementHTMLLegendElementHTMLLinkElementHTMLMapElementHTMLMediaElementHTMLMenuElementHTMLMetaElementHTMLMeterElementHTMLModElementHTMLOListElementHTMLObjectElementHTMLOptGroupElementHTMLOptionElementHTMLOptionsCollectionHTMLOutputElementHTMLParagraphElementHTMLPictureElementHTMLPreElementHTMLProgressElementHTMLQuoteElementHTMLScriptElementHTMLSelectElementHTMLSourceElementHTMLSpanElementHTMLStyleElementHTMLTableCaptionElementHTMLTableCellElementHTMLTableColElementHTMLTableElementHTMLTableRowElementHTMLTableSectionElementHTMLTemplateElementHTMLTextAreaElementHTMLTimeElementHTMLTitleElementHTMLTrackElementHTMLUListElementHTMLUnknownElementHTMLVideoElementHashChangeEventHistoryImageDataLocationMessageChannelMessageEventMessagePortNavigatorPageRevealEventPageSwapEventPageTransitionEventPluginPluginArrayPromiseRejectionEventRadioNodeListTimeRangesUserActivationValidityStateWindowWorkletGlobalScopeYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |