Open In App

HTML DOM setRangeText() method

The setRangeText() method replaces a range of text in input or a textarea with another string. 

Syntax:



element.setRangeText(replacement);

OR

element.setRangeText(replacement, start, end[, Mode]);

Parameters:



Example: In this example, we will use the setRangeText() method.




<!DOCTYPE html>
<html>
 
<head>
    <title>
        DOM focus() Method
    </title>
    <style>
        a:focus {
            background-color: magenta;
        }
    </style>
</head>
   
<body>
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <h2>DOM select() Method</h2>
    <input type="text" id="text-box"
           size="40"
           value="A online Computer Science Portal.">
    <button onclick="selectText()">
        Update text
    </button>
   
    <script>
        //Main function
        function selectText() {
            const select =
                document.getElementById('text-box');
            select.focus();
            select.setRangeText
                (' For Geeks.', 32, 40, 'select');
        }
    </script>
</body>
 
</html>

Output: 

 

Supported Browsers: The supported browsers by HTML | DOM setRangeText() method are listed below:


Article Tags :