TextArea Selection Change?
What you want is the global selectionchange
event available on the window
or document
. Read about it here.
Add a unique id to your textarea:
<textarea id="unique-id"></textarea>
Add an event listener to the document:
function handleSelection() {
const activeElement = document.activeElement
// make sure this is your textarea
if (activeElement && activeElement.id === 'unique-id') {
const range = {
start: activeElement.selectionStart,
end: activeElement.selectionEnd
}
// do something with your range
}
}
document.addEventListener('selectionchange', handleSelection)
That will run whatever code is in the handleSelection
function.
You can use the onselect
event
object.addEventListener("select", myScript);
https://www.w3schools.com/jsref/event_onselect.asp