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
Author by
chromedude
Updated on November 14, 2020Comments
-
chromedude over 3 years
I have a HTML
<title>
element which I want to dynamically change depending on other elements. I tried usingdocument.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 over 13 years
getElementsByTagName()
returns aNodeList
, an array-like object but not an actual array. -
AndreKR over 13 yearsThanks for clarification. Edited.
-
ShoeMaker over 11 yearsHow would you go about changing that Tom?
document.title.innerHTML("New value");
?