For questions about the window.getSelection() and document.getSelection() methods and the document.selectionchange and selectstart events.
Questions tagged [selection-api]
73 questions
187
votes
18 answers
Get contentEditable caret position
I'm finding tons of good, cross-browser answers on how to set the caret position in a contentEditable element, but none on how to get the caret position in the first place.
What I want to do is know the caret position within a div on keyup. So, when…

Bertvan
- 4,943
- 5
- 40
- 61
101
votes
13 answers
selectionStart/selectionEnd on input type="number" no longer allowed in Chrome
Our application uses selectionStart on input fields to determine whether to automatically move the user to the next/previous field when they press the arrow keys (ie, when the selection is at the end of the text and the user presses the right arrow…

Steven
- 3,878
- 3
- 21
- 21
44
votes
3 answers
selectionStart and selectionEnd in contenteditable element
I have been struggling the selectionStart and selectionEnd attributes of textarea to make them work with contenteditable div element. I have checked a lot of related articles on google and on SO but to no avail. I have something similar to the…

Semytech
- 623
- 1
- 10
- 17
37
votes
2 answers
selectionStart-End with textareas
I'm having this annoying problem, I can't seem to get the starting and ending index of the selected text in a textarea, all I get is undefined like this:
$('#myarea').selectionStart; // return undefined
Did I do something wrong?

Dennis
- 3,448
- 8
- 38
- 45
19
votes
4 answers
Is there an Internet Explorer approved substitute for selectionStart and selectionEnd?
Finding out what's selected in real browsers is as simple as:
var range = {
start: textbox.selectionStart,
end: textbox.selectionEnd
}
But IE, as usual, doesn't understand. What's the best cross-browser way to do this?

TALlama
- 16,017
- 8
- 38
- 47
13
votes
2 answers
How to overcome WhatWG/W3C/Chrome version 33.0.1750.146 "regression bug" with fields
I put the words "regression bug" in quotes as there is obviously some mixed opinions on this. For full details track Bug 24796 in Bugzilla.
In short Google Chrome implemented changes according to the latest version of the WhatWG specs:…

scunliffe
- 62,582
- 25
- 126
- 161
10
votes
2 answers
selectionStart for non textarea elements
$(element)[0].selectionStart seems to only work for textareas. Is there an alternative for non textareas
I'm trying to wrap strings within DOM elements in tags (bold, italic...)
Also, how do I undo the wrapping?

Zebra
- 3,858
- 9
- 39
- 52
7
votes
3 answers
What do selectionStart and selectionEnd signify for textarea?
I came across following code snippet to insert enter into the the text in a textarea where ctrl + enter is pressed.
$("#txtChatMessage").keydown(MessageTextOnKeyEnter);
function MessageTextOnKeyEnter(e) {
console.log(this.selectionEnd);
if…

vjain27
- 3,514
- 9
- 41
- 60
7
votes
2 answers
How to get selected word when double-click on div, p, span?
Can you get the word the user has double-clicked on? I've tried in a onDblClick eventhandler but selectionStart is undefined there; and the onselect event seems to be available only for TextArea.

Tim
- 429
- 6
- 19
7
votes
1 answer
setSelectionRange for input type="time"
I have a dialog box that has an HTML5 input field with a type="time" in it.
When the dialog appears, I want to default the editing to the "minutes" portion of the dialog. I can't see a way to do it.
Here is a jsFiddle example that tries to…

CleverPatrick
- 9,261
- 5
- 63
- 86
7
votes
1 answer
Getting `selectionStart` from a span's innerHTML
I have a roll your own text editor that lets you change portions of a textarea element. i want to adapt it to work with a span element. I have no particular attachment to span. The goal is simply to let someone edit html rather than a textarea. …

user1904273
- 4,562
- 11
- 45
- 96
6
votes
2 answers
Set selection range in javascript IE8
I'm working on a wysiwyg editor using div[contenteditable=true]
and I want to set a selection range from offset X of Node A to offset Y of Node B. I did it well on Firefox and IE9, the code is :
var range =…

camapcon
- 61
- 1
- 2
6
votes
1 answer
Property 'selectionStart' does not exist on type 'EventTarget'
I am using selectionStart and selectionEnd in order to get the starting and ending point of a text selection.
Code: https://codesandbox.io/s/busy-gareth-mr04o
However, I am struggling to defined the type of the event on which they can be called…

Magofoco
- 5,098
- 6
- 35
- 77
5
votes
2 answers
Find character position based on mouse move in a input field
I need to find the position of the character in the input field based on the mouse movement.
for example: if the input field has value as 'abcde' and I am moving my
mouse on the character 'a' then the position will be 1, 'b' will provide 2 and so…

Vidushi Gupta
- 101
- 8
5
votes
3 answers
Failed to read the 'selectionStart' property from 'HTMLInputElement':
I'm using binding handler.if i remove this code my code is saved.but if i use
this code it will throw error.
Uncaught InvalidStateError: Failed to read the 'selectionStart' property from 'HTMLInputElement': The input element's type ('checkbox')…

diy
- 119
- 1
- 1
- 10