You are here: Reference > JavaScript > client-side > selection and ranges > methods > cloneRange (Range)

cloneRange method (Range)

Browser support:
9
Returns an exact copy of the current Range object.
Note: The Range object and its cloneRange method are supported in Internet Explorer from version 9.
In Internet Explorer before version 9 (and in newer ones as well), the duplicate method of the TextRange object provides similar functionality.

Syntax:

object.cloneRange ( );
You can find the related objects in the Supported by objects section below.

Return value:

Returns the newly created Range object.

Example HTML code 1:

This example illustrates the use of the cloneRange method:
<head>
    <script type="text/javascript">
        function CloneARange () {
            if (document.createRange) {     // all browsers, except IE before version 9
                var rangeObj = document.createRange ();
                rangeObj.selectNodeContents (document.body);
                var rangeClone = rangeObj.cloneRange ();
                alert (rangeClone.toString ());
            }
            else {  // Internet Explorer before version 9
                if (document.body.createTextRange) {
                    var rangeObj = self.document.body.createTextRange ();
                    var rangeClone = rangeObj.duplicate ();
                    alert (rangeClone.htmlText);
                } 
            }
        }
    </script>
</head>
<body>
    <button onclick="CloneARange ();">Clone a range!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content