Open In App

HTML | DOM Title text Property

Improve
Improve
Like Article
Like
Save
Share
Report

The DOM Title text Property is used to set or return the text of the title of the document

Syntax: 

  • It returns the text property.
titleObject.text
  • It is used to set the text property.
titleObject.text = text

Property Values: It contains the value i.e text which is used to specify the text of the document’s title. 

Return Value: It returns a string value which represents the text of the document’s title. 

Example-1: This Example returns a text Property

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        DOM Title text Property
    </title>
</head>
 
<body style="text-align:center;">
 
    <h1 style="color:green;">
            GeeksforGeeks
        </h1>
 
    <h2>
      DOM Title text Property
  </h2>
 
    <p>
      Welcome to GeeksforGeeks!
  </p>
 
    <button onclick="myGeeks()">
        Submit
    </button>
 
    <p id="sudo"></p>
 
    <!-- script to display the text of the document's title -->
    <script>
        function myGeeks() {
            var g = document.getElementsByTagName(
              "TITLE")[0].text;
           
            document.getElementById(
              "sudo").innerHTML = g;
        }
    </script>
</body>
 
</html>


Output: 

Before Clicking On Button:

  

After Clicking On button:

  

Example-2: This Example sets the text Property

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
         DOM Title text Property
    </title>
</head>
 
<body style="text-align:center;">
 
    <h1 style="color:green;">
            GeeksforGeeks
        </h1>
 
    <h2>
      DOM Title text Property
  </h2>
 
    <p>
      Welcome to GeeksforGeeks!
  </p>
 
    <button onclick="myGeeks()">
        Submit
    </button>
 
    <p id="sudo"></p>
 
    <!-- script to change the text of the document's title -->
    <script>
        function myGeeks() {
            var g =
                document.getElementsByTagName(
                  "TITLE")[0].text = "GeeksForGeeks";
           
            document.getElementById("sudo").innerHTML =
              "The title was changed to " + g;
        }
    </script>
</body>
 
</html>


Output : 

Before Clicking On button:

  

After Clicking On button: 



Last Updated : 17 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads