Is there a javascript function that will allow me to capture the text that is currently highlighted with the cursor and store it in a variable? I\'ve been trying document.se
Ungraciously stolen from another question:
function getSelectedText() {
if (window.getSelection) {
return window.getSelection();
}
else if (document.selection) {
return document.selection.createRange().text;
}
return '';
}
Use this in a "onClick" function or whatever and it will return the selected text in almost any browser.
function getSelectedText() {
if (window.getSelection) {
return "" + window.getSelection();
} else if (document.selection && document.selection.type == "Text") {
return document.selection.createRange().text;
}
return "";
}
Yep, you want window.getSelection.