Open In App

HTML DOM createRange() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The createRange() method creates a new Range object for the document.

Syntax:

range = document.createRange();

Parameters: This method does not accept any parameters.

Return Value: This method returns the created Range.

Example: In this example, we will create a range using this method, a range should not be empty, so we will make starting and ending points in the range using setEnd() and setStart() method.

HTML




<!DOCTYPE html>
<html>
  
<body>
    <h1>GeeksforGeeks</h1>
    <p id="parent">
        Child 1<br>
        Child 2<br>
    </p>
  
    <script>
        const example = document.getElementById('parent');
        const range = document.createRange();
        range.setStart(example, 0);
        range.setEnd(example, 3);
        console.log(range);
        console.log(range.toString());
    </script>
</body>
  
</html>


Output: In console, created range can be seen.

Supported Browsers:

  • Google Chrome
  • Edge
  • Firefox
  • Safari
  • Opera

Last Updated : 29 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads