Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTMLTextAreaElement.selectionStart — selection start

HTMLTextAreaElement.selectionStart reads or sets the selection's start in a textarea. Its zero-based offset counts UTF-16 code units rather than visible characters.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

textarea.selectionStart
textarea.selectionStart = offset

HTMLTextAreaElement.selectionStart is a writable property of a textarea control. Together with selectionEnd, it defines a range: the start is inclusive and the end exclusive. Without a selection, both values indicate the caret.

Offsets count UTF-16 code units in value: 😀 occupies two. On assignment, Web IDL first converts the value to an unsigned 32-bit integer. The position is clamped to the end of the text only if the converted value exceeds the text length. For example, assigning 2 ** 32 sets the start to 0 because the conversion yields 0. If the resulting start is beyond the existing end, the end moves to the same position, collapsing the selection.

This is a browser API from the HTML Standard, not an ECMAScript feature; a Node.js version alone does not establish support. The example uses a detached control: its selection state can be read without rendering.

Examples

const textarea = document.createElement('textarea');
textarea.value = 'A😀BC';
textarea.setSelectionRange(1, 3);
console.log(textarea.selectionStart, textarea.selectionEnd);
console.log(textarea.value.slice(textarea.selectionStart, textarea.selectionEnd));
textarea.selectionStart = 4;
console.log(textarea.selectionStart, textarea.selectionEnd);

Result

1 3
😀
4 4

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources