setRangeText Edits Input Selections
HTMLInputElement.setRangeText replaces a character range with four selection modes, and omitting start and end targets the current user selection instead.
Code
declare const input: HTMLInputElement;
// 完整签名:替换 [start, end) 并声明替换后的选区位置
input.setRangeText('REDACTED', 3, 7, 'select');
console.log('select keeps the replacement highlighted');
// 'start' / 'end' 把光标放到插入点前后;缺省 'preserve' 尽量保住原选区
input.setRangeText('-', 0, 0, 'end');
const mode: 'select' | 'start' | 'end' | 'preserve' = 'preserve';
input.setRangeText('x', 0, 1, mode);
console.log('selection mode union is closed:', mode);
// 只传替换串:范围取用户当前选区 selectionStart/selectionEnd
input.setRangeText('PASTED');
console.log(input.value);Using it
The run emits 2 labeled sections — select keeps the replacement highlighted, selection mode union is closed: — one per step.
Output is labeled for readability — expect lines prefixed with select keeps the replacement highlighted, selection mode union is closed:.
Key statements
const mode: 'select' | 'start' | 'end' | 'preserve' = 'preserve';
input.setRangeText('REDACTED', 3, 7, 'select');
declare const input: HTMLInputElement;
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Credit Card Brand DetectorIdentify the card network from a number's prefix and length, validate the Luhn checksum, and show a masked form and the issuer identification number.
FAQ
- Is there an install step between copy and run?
- No. Between copying and running there is nothing left to do.
Report a problem with this exampleOpens a pre-filled issue on GitHub.