References

Beginner-friendly references for web development, with live, editable examples.

The HTML selectionStart attribute

Attribute All modern browsers Updated
Quick answer

The selectionStart is a JavaScript property (not an HTML attribute) of text fields giving (or setting) the character index where the current selection begins.

Overview

The selectionStart attribute start of the text selection (a JS property). It is used on media and form elements — though it is a JavaScript property, not an attribute you write in markup.

Despite appearing in attribute lists, this is a JavaScript IDL property, not an HTML attribute you write in the markup. You read (or set) it on the element in script, for example to inspect playback or selection state.

Syntax

input.selectionStart = 0;\ninput.selectionEnd = input.value.length;  // select all

Values

Value
An integer character offset.

Best practices

  • Access it in JavaScript on the element object — there is no corresponding HTML attribute to write.
  • Read it to inspect state; some are read-only and cannot be set.
  • Do not try to set it as a markup attribute — it will be ignored.
  • Combine it with the relevant events to react to changes.

Frequently asked questions

What does the selectionStart attribute do?
Start of the text selection (a JS property).
Can I set this as an HTML attribute?
No. It is a JavaScript property, not a markup attribute. Read or set it on the element in script.
How do I access it?
Through the element object in JavaScript, e.g. element.property.
Which elements use the selectionStart attribute?
It is an element-specific attribute, used on media and form elements — though it is a JavaScript property, not an attribute you write in markup.