window.getSelection().toString()
worked for me with Chrome but not Firefox.
For obtaining the selected content in a <textarea>
with Firefox:
function getSel() // javascript
{
// obtain the object reference for the <textarea>
var txtarea = document.getElementById("mytextarea");
// obtain the index of the first selected character
var start = txtarea.selectionStart;
// obtain the index of the last selected character
var finish = txtarea.selectionEnd;
// obtain the selected text
var sel = txtarea.value.substring(start, finish);
// do something with the selected content
}
You could also use activeElement instead of getElementById.
Reference:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…