Questions tagged [getselection]
198 questions
61
votes
3 answers
Get caret (cursor) position in contentEditable area containing HTML content
I have contentEditable element (can be p, div, ...) and I would like to get caret (cursor) position in it. I can normally achieve it with this piece of code:
var position = window.getSelection().getRangeAt(0).startOffset;
This works fine while the…

Frodik
- 14,986
- 23
- 90
- 141
41
votes
5 answers
How to get selected html text with javascript?
I can use the following code to get selected text:
text=window.getSelection(); /// for Firefox
text=document.selection.createRange().text; /// for IE
But how can I get the selected Html, which includes the text and html tags?

user570494
- 557
- 3
- 8
- 14
35
votes
12 answers
What's the best way to set cursor/caret position?
If I'm inserting content into a textarea that TinyMCE has co-opted, what's the best way to set the position of the cursor/caret?
I'm using tinyMCE.execCommand("mceInsertRawHTML", false, content); to insert the content, and I'd like set the cursor…

Daniel Bachhuber
- 703
- 2
- 10
- 20
21
votes
7 answers
Javascript selected text highlighting prob
I have a html page with text content. On selecting any text and pressing the highlight button, I can change the style of the selected text to highlight the same. To implement this feature, i have written the following method.
sel =…

dev_android
- 8,698
- 22
- 91
- 148
17
votes
3 answers
How to find index of selected text in getSelection() using javascript?
I am trying to apply style to the text selected by the user(mouse drag) for which I need to get the start and end index of the selected text.
I have tried using "indexOf(...)" method. but it returns the first occurrence of the selected substring. I…

Keekz
- 185
- 2
- 10
12
votes
2 answers
getSelection & surroundContents across multiple tags
I've got a script that changes the background colour of text that has been selected. However i'm encountering an issue when the text is selected across multiple elements/tags.
The code that i've got is:
var text =…

lethalbody
- 123
- 1
- 1
- 4
9
votes
3 answers
How to support multiple text selections cross browser?
I am building a web-based annotation tool, where end users can select and annotate a section of text from an HTML document. Programmatically speaking, accessing and working with the selected text and corresponding range is straightforward using…

user14636
- 398
- 5
- 9
8
votes
2 answers
window.getSelection() for contenteditable div *on click*
I have a contenteditable div and want to get the user's selection when they click a span.
My problem is that when I click the span, the selection gets unselected so window.getSelection().toString() returns ''.
How can I get this to work on click of…

ᔕᖺᘎᕊ
- 2,971
- 3
- 23
- 38
8
votes
1 answer
how do I get co-ordinates of selected text in an html using javascript document.getSelecttion()
I would like to position element above selected text. But I am not able to figure out the coordinates.
var sel = document.getSelection();
if(sel != null) {
positionDiv();
}
Example: (image)

priyank
- 4,634
- 11
- 45
- 52
7
votes
3 answers
Mimic Ctrl+A with JavaScript
I would like to select all text on a page programmatically with the exact same result as if I would press key combo Ctrl+A.
The problem with using document.getSelection().selectAllChildren(body) is that the selection will also include text nodes…

user1521685
- 210
- 2
- 13
7
votes
2 answers
How to get the coordinates of the end of selected text with javascript?
My problem is similar to this, but I need a way to get the coordinates of the right side of the selection with Javascript in Firefox. I made a small example to show what I mean:
The code I got from the other post is the following:
var range =…

Bob
- 999
- 1
- 9
- 12
7
votes
2 answers
Need to set cursor position to the end of a contentEditable div, issue with selection and range objects
I'm forgetting about cross-browser compatibility for the moment, I just want this to work.
What I'm doing is trying to modify a script (and you probably don't need to know this) located at typegreek.com The basic script is found here. Basically what…

DavidR
- 5,350
- 6
- 30
- 36
7
votes
2 answers
Get Chosen App from Intent.createChooser
I am trying to capture the result of Intent.createChooser to know which app a user selected for sharing.
I know there have been a lot of posts related to this:
How to know which application the user chose when using an intent…

dsg
- 12,924
- 21
- 67
- 111
6
votes
2 answers
Get the raw HTML of selected content using javascript
How would I get the raw HTML of the selected content on a page using Javascript? For the sake of simplicity, I'm sticking with browsers supporting window.getSelection.
Here is an example; the content between both | represent my selection.
…

Jon Neal
- 76
- 3
6
votes
1 answer
getSelection() broken in IE10
I use the window.getSelection() method on my project to make a quotable text
It's working just fine in all modern browsers, exept IE10. In IE10 console returns correct text, but the selection is broken.
The only code I used:
text =…

Bohdan Khodakivskyi
- 634
- 5
- 14