I have a content editable div. So I need to call to a function when any text is selected inside of that div. Is it possible to trigger a function when text is selected by mouse and keyboard too.
Asked
Active
Viewed 199 times
0
-
I use this function, hope it helps :) http://jsfiddle.net/Lgbnzax8/ – Jaapaap Jan 09 '19 at 12:01
1 Answers
2
The DOM API has the selectionchange
event for that:
https://developer.mozilla.org/en-US/docs/Web/Events/selectionchange
Unfortunately an even listener for that event can only be attached to the document
object, and not to any specific element.
So what you can do is attach the listener in the mounted()
hook:
mounted() {
document.addEventListener('selectionchange', this.handleSelectionChange);
}
I've created a small sample project showing the usage. Unforunately the event target is always document
.

connexo
- 53,704
- 14
- 91
- 128
-
It's not fire any function. Is it a problem of Browser compatibility? or I have more events in that div so those events prevent that? – Shanaka Nuwan Jan 09 '19 at 12:25
-
@ShanakaNuwan Check my updated answer. Unfortunately my first approach won't do anything. – connexo Jan 09 '19 at 12:50
-