Reputation: 4302
I have a HTML <title>
element which I want to dynamically change depending on other elements. I tried using document.getElementsByTagName('title').innerHTML = dynamicContent
but this did not seem to work. I have seen it done before, but I can't seem to figure out exactly how to do this.
Upvotes: 3
Views: 23951
Reputation: 186762
You can manipulate
a) document.title = 'blah';
b) .textContent or .innerText depending on the browser
Upvotes: 1
Reputation: 243096
Do you mean the <title>
element in <head>
of the page?
If yes, then changing document.title
should do the trick.
Upvotes: 17
Reputation: 33707
getElementsByTagName()
returns a NodeList, so you need to pick one element:
document.getElementsByTagName('title')[0].innerHTML = dynamicContent
There's also a shortcut to the title:
document.title = dynamicContent
Upvotes: 7