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

21,431

Solution 1

Do you mean the <title> element in <head> of the page?
If yes, then changing document.title should do the trick.

Solution 2

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

Solution 3

You can manipulate

a) document.title = 'blah';

b) .textContent or .innerText depending on the browser

Share:
21,431
chromedude
Author by

chromedude

Updated on November 14, 2020

Comments

  • chromedude
    chromedude over 3 years

    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.

  • Tim Down
    Tim Down over 13 years
    getElementsByTagName() returns a NodeList, an array-like object but not an actual array.
  • AndreKR
    AndreKR over 13 years
    Thanks for clarification. Edited.
  • ShoeMaker
    ShoeMaker over 11 years
    How would you go about changing that Tom? document.title.innerHTML("New value");?