+ 2
[SOLVED] How to get the index of the selected text in a contenteditable div?
The title is pretty self-explanatory. I tried using div.selectionStart, but it only works for inputs and textareas. I know that one way would be to find the index of the selected text, but it wonât work if the selected text is repeated earlier in the div. Do you know any way to do it? Thanks in advance.
10 Answers
+ 2
Like, when you call this, it returns object with selection.
More info:
https://developer.mozilla.org/cs/docs/Web/API/Window/getSelection
+ 6
Maneren Thanks for that link!
+ 5
Maneren Can you please give an example?
+ 3
So, i ran out of ideas
Only that with css
+ 2
What do you want to do with the text after it is selected, for something you can use css selectors
+ 2
In css, and now i got an idea, in javascript, one can use querySelector() with div::selection selector, am i right?
+ 1
Maneren Not sure. I just think it would be useful knowing how to get the selection index.
+ 1
Hmm... I think that the querySelector function gets elements, whereas the selected text would only be part of an element, not the whole thing.
+ 1
window.getSelection(); should do the trick
+ 1
You're welcome