Here selObj is a Selection object
Calling the Selection/toString() method returns the text contained in the selection, e.g
window.alert(selObj)
Note that using a selection object as the argument to window.alert will call the object's toString method.
Browser Compatibility:
Browser | support |
---|---|
Chrome | Yes |
Firefox (Gecko) | yes |
Internet Explorer | yes (tested IE7+) |
Opera | yes |
Safari | yes |
Cross-browser solution to get the content of the selected content:
function testSelection () {
if (window.getSelection) { // all browsers, except IE before version 7
var selectionRange = window.getSelection ();
alert ("The text content of the selection:\n" + selectionRange.toString ());
}
else {
if (document.selection.type == 'None') {
alert ("No content is selected, or the selected content is not available!");
}
else {
var textRange = document.selection.createRange ();
alert ("The text content of the selection:\n" + textRange.text);
}
}
}
</script>
References:
Mozilla Developer's Network
MSDN