How to change <title> element using javascript?

I have an HTML element <title>that I want to dynamically change depending on other elements. I tried using document.getElementsByTagName('title').innerHTML = dynamicContent, but that didn't seem to work. I have seen this before, but I cannot figure out exactly how to do this.

+3
source share
3 answers

Do you mean the element <title>in <head>on the page?
If so, the change document.titleshould do the trick.

+13
source

getElementsByTagName() returns a NodeList, so you need to select one element:

document.getElementsByTagName('title')[0].innerHTML = dynamicContent

There is also a shortcut for the title:

document.title = dynamicContent
+7
source

a) document.title= 'blah';

b).textContent .innerText

+1

Source: https://habr.com/ru/post/1774743/


All Articles