Javascript Reference - HTML DOM Title text Property








The text property sets or gets the text of the document's title.

Browser Support

text Yes Yes Yes Yes Yes

Syntax

Return the text property.

var v = titleObject.text 

Set the text property.

titleObject.text=text

Property Values

Value Description
text Set the title text




Return Value

A String type value representing the text of the document's title.

Example

The following code shows how to get the document's title.


<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Objects</title>
</head><!-- ww  w  . j  a  v  a  2 s  . co m-->
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementsByTagName("TITLE")[0].text;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the document's title.


<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Objects</title>
</head><!--from w ww  .  jav  a 2s . co m-->
<body>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementsByTagName("TITLE")[0].text = "A new title text..";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows: