I want to implement a JavaScrit selectionchange
event on a particular div
element, so if the user selects text from the DOM I want to show a highlighter box. I have implemented this for web with an onmouseup
event. But I am stuck trying to implement this for mobile devices.
For mobile browsers I am binding document.selectionchange
event on the DOM, but I want this to only apply for a particular element which has the content-editable
class. So the highlighter will show only when the user selected text within the container on page which has the content-editable
class.
document.addEventListener("selectionchange", function(evt) {
// Now this functionality only apply for content-editable class div.
});
How can I implement this functionality? Perhaps it could be implemented with a recursive function to find parentElement
's class of anchorNode
of selected text like:
var selection = ctrl.window.getSelection();
What is the best way to do this?