Open In App

HTML DOM insertAdjacentText() Method

The insertAdjacentText() inserts a provided text at one of the following positions.

Syntax:



node.insertAdjacentText(position, text)

Parameters: This method requires 2 parameters.

Return Value: No Return Value. 



Exceptions: If the specified position is not recognized. 

Example: In this example, we will use insertAdjacentText().




<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML DOM insertAdjacentText() Method
    </title>
</head>
 
<body>
    <h1> Welcome To GeeksforGeeks</h1>
    <strong>
        <p id="m1">GeeksforGeeks is a </p>
    </strong>
    <p>
        Click the button to insert some
        text after the sentence:
    </p>
    <button onclick="insadjtxt()">
        Insert text
    </button>
   
   <!--script to insert specified
        element to specified position-->
    <script>
        function insadjtxt() {
            let h = document.getElementById("m1");
            h.insertAdjacentText("beforeend",
                " Computer Science Portal.");
        }
    </script>
</body>
 
</html>

Output: 

 

Supported Browsers: The browser supported by DOM insertAdjacentText() Method are listed below:


Article Tags :