JavaScript - getSelection() method

revision:


Category : document

The getSelection() method of the Document interface returns a Selection object representing the range of text selected by the user, or the current position of the caret.

Syntax :

        getSelection()
    

Parameters: none

Examples:

            let selection = document.getSelection();
            let selRange = selection.getRangeAt(0);
            // do stuff with the range
            console.log(selection); // Selection object
        

Practical examples

example:
code:
                    
                

example:
code:
                    
                

example:
code: