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>neveropen</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

