How to get the Highlighted/Selected text in JavaScript?
There may be a need to find out the text selected/highlighted by the user. It can be done very easily using the window and document objects and their properties. Handling selected text is different for different browsers. The ways to get selected text are shown below:
Example-1: By using window.getSelection property
function selection(){
if (window.getSelection)
return window.getSelection();
}
Example 2: By using document.getSelection property
function selection(){
if (document.getSelection)
return document.getSelection();
}
Example 3: By using document.selection property
function selection(){
if (document.selection)
return document.selection.createRange().text;;
}
Time to try out the code. Run the code, select a text and press the button to show the selected text:
Example:
<html> <head> <title>Selected Text</title> </head> <body> <center> <h1 style=color:green> GeeksfroGeeks </h1> <script> // Function to get the Selected Text function getSelectedText() { var selectedText = ''; // window.getSelection if (window.getSelection) { selectedText = window.getSelection(); } // document.getSelection else if (document.getSelection) { selectedText = document.getSelection(); } // document.selection else if (document.selection) { selectedText = document.selection.createRange().text; } else return; // To write the selected text into the textarea document.testform.selectedtext.value = selectedText; } </script> <p>Select any part of this sentence and press the button</p> <!--Button to invoke the function to get the selected text--> <input type="button" value="Get Selection" onmousedown="getSelectedText()"> <!--Form to show the selected text as output--> <form name="testform"> <textarea name="selectedtext" rows="5" cols="20"></textarea> </form> </center> </body> </html> |
Output:
Before:

After:

Recommended Posts:
- How to select all text in HTML text input when clicked using JavaScript?
- JavaScript | Get the text of a span element
- How to get the value of text input field using JavaScript?
- How to remove text from a string in JavaScript?
- How to process each letter of text using JavaScript ?
- How to copy the text to the clipboard in JavaScript?
- Calculate the width of the text in JavaScript
- How to change the text of a label using JavaScript ?
- Converting JSON text to JavaScript Object
- JavaScript | Change the text of a span element
- Javascript | Program to read text File
- JavaScript | Program to write data in a text File
- How to change text color depending on background color using JavaScript?
- CSS | Text Effects
- How to replace text with CSS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


