chromedude
chromedude

Reputation: 4302

How do you change the <title> element with javascript?

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

Answers (3)

meder omuraliev
meder omuraliev

Reputation: 186762

You can manipulate

a) document.title = 'blah';

b) .textContent or .innerText depending on the browser

Upvotes: 1

Tomas Petricek
Tomas Petricek

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

AndreKR
AndreKR

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

Related Questions