On Text Highlight Event?
There isn't any onhighlightext
or anything like that, but a solution would be to bind onmouseup
to check if any text is selected if this isn't in a input
/textarea
.
Edit
Here's an implementation example for you. I only tested this in Chrome/Firefox/IE7. This works in inputs as well.
http://jsfiddle.net/qY7gE/
Code from JSFiddle:
var t = '';
function gText(e) {
t = (document.all) ? document.selection.createRange().text : document.getSelection();
document.getElementById('input').value = t;
}
document.onmouseup = gText;
if (!document.all) document.captureEvents(Event.MOUSEUP);
<input type='text' id='input' />
In software, a stack overflow occurs when too much memory is used on the call stack. The call stack contains a limited amount of memory, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory. When too much memory is used on the call stack the stack is said to overflow, typically resulting in a program crash.[1] This class of software bug is usually caused by one of two types of programming errors.[2]
There is a native event for when a text selection is made/changed. selectionchange
has basic support on most browsers, including IE, and will work for any text within a document not just form elements.
document.addEventListener("selectionchange",event=>{
let selection = document.getSelection ? document.getSelection().toString() : document.selection.createRange().toString() ;
console.log(selection);
})
select this text
Note, as its name implies it fires on any change of selection. So you will get multiple calls to your callback function as you select text.