Given a web page containing the page title and the task is to change the title of a web page dynamically using JavaScript.
Method 1: Using document.title property: The document.title property is used to set or return the current title of the document. The title of the page can be changed by assigning the new title as a string to this property. This will change the title of the website to the preferred title.
Syntax:
newPageTitle = 'The title has changed!'; document.title = newPageTitle; |
Example:
<!DOCTYPE html> <html> <head> <title> How to dynamically change a web page’s title using JavaScript ? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to dynamically change a web page’s title using JavaScript ? </b> <p> Click on the button to change the page title to: "The title has changed!" </p> <button onclick="changePageTitle()"> Change Page Title </button> <script type="text/javascript"> function changePageTitle() { newPageTitle = 'The title has changed!'; document.title = newPageTitle; } </script> </body> </html> |
Output:
- Before clicking the button:

- After clicking the button:

Method 2: Using querySelector() Method: The document.querySelector() method is used to select elements in the document. The title element can be selected by specifying the title element in the selector as a parameter. This will return the current title element of the page.
The textContent property of an element returns the text content of a specific node. The title of the page can be changed by assigning the required new title as a string to the textContent property. This will change the title of the website to the preferred title.
Syntax:
newPageTitle = 'The title has changed!'; document.querySelector('title').textContent = newPageTitle; |
Example:
<!DOCTYPE html> <html> <head> <title> How to dynamically change a web page’s title using JavaScript ? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to dynamically change a web page’s title using JavaScript ? </b> <p> Click on the button to change the page title to: "The title has changed!" </p> <button onclick="changePageTitle()"> Change Page Title </button> <script type="text/javascript"> function changePageTitle() { newPageTitle = 'The title has changed!'; document.querySelector('title').textContent = newPageTitle; } </script> </body> </html> |
Output:
- Before clicking the button:
- After clicking the button:
Recommended Posts:
- How to change style attribute of an element dynamically using JavaScript ?
- How to Change Image Dynamically when User Scrolls using JavaScript ?
- Web 1.0, Web 2.0 and Web 3.0 with their difference
- How to get title of current HTML page using jQuery ?
- How to set or update page title using UI-Router?
- How to disable right click on web page using JavaScript ?
- How to change favicon dynamically?
- Change Image Dynamically when User Scrolls Down | Set 2
- How to get the title of an HTML page ?
- How to display search result of another page on same page using ajax in JSP?
- How to create an image element dynamically using JavaScript ?
- How to Change the Style of <a> Tag Title Attribute ?
- How to hide an element when printing a web page using CSS?
- Download web page using Java
- How to disable zoom on a mobile web page using CSS?
- How to get the size of screen, current web page and browser window using jQuery?
- What are the differences between web-garden and a web-farm in JavaScript?
- How to show Page Loading div until the page has finished loading?
- How to redirect a page to another page in HTML ?
- How to pass form variables from one page to other page in PHP ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


