JavaScript – Get selected highlighted text
The code:
<!DOCTYPE html> <html> <head> <script> function ShowSelection() { var textComponent = document.getElementById('Editor'); var selectedText; // IE version if (document.selection != undefined) { textComponent.focus(); var sel = document.selection.createRange(); selectedText = sel.text; } // Mozilla version else if (textComponent.selectionStart != undefined) { var startPos = textComponent.selectionStart; var endPos = textComponent.selectionEnd; selectedText = textComponent.value.substring(startPos, endPos) } alert("You selected: " + selectedText); } </script> </head> <body> Enter your name: <input type="text" id="Editor"> <button onclick="ShowSelection()">ShowSelection()</button> </body> </html>