Javascript Reference - HTML DOM Title Object








The Title object represents an HTML <title> element.

Title Object Properties

Property Description
text Sets or gets the text of the document's title

Standard Properties and Events

The Title object supports the standard properties and events.

Example

We can get a <title> element by using getElementsByTagName().


<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Objects</title>
</head><!--  ww w  .j a v a  2  s .com-->
<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

We can create a <title> element by using the document.createElement() method.


<!DOCTYPE html>
<html>
<head>
</head><!--  ww  w  .j ava2  s. com-->
<body>
<button onclick="myFunction()">test</button>

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

<script>
function myFunction() {
    var x = document.createElement("TITLE");
    var t = document.createTextNode("HTML DOM Objects");
    x.appendChild(t);
    document.head.appendChild(x);

    document.getElementById("demo").innerHTML = "added";
}
</script>

</body>
</html>

The code above is rendered as follows: