The Title object represents an HTML <title> element.
Property | Description |
---|---|
text | Sets or gets the text of the document's title |
The Title object supports the standard properties and events.
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:
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: