HTML DOM Range selectNodeContents() Method

The selectNodeContents() method sets the Range that contain the contents of a Node.

The startOffset is 0 every time, and the endOffset can be the number of child Nodes or number of characters contained in the reference Node.

Syntax:

range.selectNodeContents(referenceNode);

Parameters:

  • referenceNode : The Node whose contents will set the Range Content.

Return Value: This method has no return value.



Example: This example shows how to set the content of the range using this method.

For better clarification of the example , console logged the range content in string text by toString() method.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML DOM range selectNodeContents() method
    </title>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <div>This is the Range Content</div>
  
    <script>
        var range;
        range = document.createRange();
        range.selectNodeContents(
            document.getElementsByTagName("div")[0]);
        console.log(range);
        console.log(range.toString());
    </script>
</body>
  
</html>

chevron_right


Output: In console, range can be seen.

Supported Browsers: The browsers supported by DOM selectNodeContents() method are listed below:

  • Google Chrome
  • Edge
  • Firefox
  • Safari
  • Opera
  • 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.