Tuesday, July 14, 2026
HomeLanguagesJavascriptHow to get the Highlighted/Selected text in JavaScript?

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: 

Method 1: window.getSelection property: The window.getSelection method returns a selection object that represents the text selection made by the user or the caret’s current position.

Syntax:

function selection(){
if (window.getSelection)
       return window.getSelection();
}

Method 2: document.getSelection property: The method of document interface returns a selection object that represents the user-selected text.

Syntax:

function selection(){
if (document.getSelection)
       return document.getSelection();
}

Method 3: document.selection property: This method also returns a selection object that displays the text selected by the user.

Syntax:

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: In this example, we will use all three methods to get highlighted or selected text.

HTML




<h1 style=color:green>
    neveropen
</h1>
 
<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>
<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>


Output:

How to get the Highlighted/Selected text in JavaScript?

How to get the Highlighted/Selected text in JavaScript?

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6901 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12111 POSTS0 COMMENTS
Shaida Kate Naidoo
7022 POSTS0 COMMENTS
Ted Musemwa
7263 POSTS0 COMMENTS
Thapelo Manthata
6978 POSTS0 COMMENTS
Umr Jansen
6968 POSTS0 COMMENTS