HTML | DOM textContent Property

The textContent property in HTML is used to set or return the text content of specified node and all its descendants. This property is very similar to nodeValue property but this property returns the text of all child nodes.

Syntax:

  • It is used to set the text of node.
    node.textContent = text
  • It is used to return the text of node.
    node.textContent

Property Value: It contains single value text which contains node the node content.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
    <head
        <title>
            HTML DOM textContent Property
        </title
    </head
    <body
        <h1>GeeksforGeeks</h1
        <h2>HTML DOM textContent Property</h2>
          
        <button id = "geeks" onclick = "MyGeeks()">
            Submit
        </button
          
        <p id = "sudo"></p
        <script
        function MyGeeks() { 
            var text = document.getElementById("geeks").textContent;
            document.getElementById("sudo").innerHTML = text;
        
        </script
    </body
</html>                     

chevron_right


Output:
Before Click on the button:

After Click on the button:



Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
    <head
        <title>
            HTML DOM textContent Property
        </title
    </head
    <body
        <h1>GeeksforGeeks</h1
        <h2>HTML DOM textContent Property</h2>
          
        <p id = "geeks" onclick = "MyGeeks()">
            Hello Geeks!
        </p
          
        <script
            function MyGeeks() { 
                document.getElementById("geeks").textContent 
                = "Welcome to GeeksforGeeks!";
            }
        </script
    </body
</html>                     

chevron_right


Output:
Before Click on the text:

After Click on the text:

Supported Browsers: The browser supported by DOM textContent property are listed below:

  • Chrome 1.0
  • Internet Explorer 9.0
  • Firefox
  • Safari
  • Opera


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