HTML | DOM setRangeText() method

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




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


  • replacement: This parameter represents a string that will replace with the exits one.
  • Start: It specifies the index of the first character and it’s optional.
  • end: It specifies the index of character after the last start character it’s also optional.
  • Mode: This attribute defines how the selection should be set all the possible values are described below.
    • select: This will select the newly inserted text.
    • start: It will move the selected text before the newly inserted text.
    • end: Similar to start just move the selected text after the inserted text.
    • preserve: Preserve the selection and its default.
  • Example:





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



    Output :
    Before clicking the Update button:

    After clicking Update button.

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

    • Google Chrome
    • Internet Explorer
    • Firefox
    • Opera
    • Safari


    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 or mail your article to 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 to report any issue with the above content.