HTML DOM selection.type property

The type property returns a String which describes the type of current selection. This is a read only property.

Syntax:

selection.type

Return Value: String describing type of the selection. Possible return values are:

  • None: No selection has currently been made.
  • Caret: Only Clicked but not selected. The selection is collapsed, the caret is placed on some text, but no range has been selected.
  • Range: Some range on which the text has been selected.

Example:

In this example, getSelection() method is used to get the selected text and type property is used to get the type of selection.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
<head>
<title>HTML DOM selection type property</title>    
</head>
<body>
    <h1>GeeksforGeeks</h1>
    <p>select some text and see its type in console</p>
</body>
<script>
    var selection;
    document.onselectionchange = function() {
      selection = document.getSelection();
      console.log(selection)
      console.log(selection.type);
    };
</script>
</html>

chevron_right


Output:

In console, types of selections can be seen on every change of selection.

Supported Browsers:

  • Google Chrome
  • Edge
  • Firefox
  • Opera
  • Safari
  • Internet Explorer

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.