• Javascript
  • Python
  • Go

Changing the <title> Element with JavaScript

The &lt;title&gt; element is a fundamental part of every HTML document. It is used to define the title of the page, which appears at the top...

The <title> element is a fundamental part of every HTML document. It is used to define the title of the page, which appears at the top of the browser window and is also used as the default name of the bookmark when a user saves the page.

Traditionally, the <title> element is set in the <head> section of an HTML document and is static, meaning it does not change unless the HTML code is manually updated. However, with the rise of dynamic and interactive web applications, there is a need to change the <title> element dynamically using JavaScript.

There are several scenarios in which changing the <title> element with JavaScript can be useful. For example, in a single-page application, the <title> element can be updated to reflect the current page or section the user is on, providing a better user experience. Additionally, in an e-commerce website, the <title> element can be changed to include the name of the product the user is viewing, making it easier for them to find and reference later.

So, how can we change the <title> element with JavaScript? Let's take a look at a simple example.

First, we need to access the <title> element in our HTML document. This can be done using the document object and the getElementById() method. We give our <title> element an id of "title" so we can easily target it.

Once we have access to the <title> element, we can use the innerHTML property to change its content. For example, if we want to change the <title> to "My Website - Home", we can use the following code:


document.getElementById("title").innerHTML = "My Website - Home";


This will update the <title> element to display the new title in the browser window and in the bookmark.

But what if we want to change the <title> element dynamically based on user input or a specific event? In this case, we can use event listeners to trigger the change in the <title> element.

For instance, if we have a search bar on our website and we want the <title> element to display the search term the user entered, we can use a keyup event listener on the search input field. This will listen for any key being pressed and then update the <title> element accordingly.


document.getElementById("search-input").addEventListener("keyup", function() {

document.getElementById("title").innerHTML = "My Website - Search Results for: " + document.getElementById("search-input").value;



In this example, we are using string concatenation to add the search term to the <title> element.

Another useful scenario for changing the <title> element with JavaScript is when creating a multi-lingual website. Instead of having a static <title> for each language, we can use JavaScript to update the <title> based on the user's preferred language.


if (userLanguage === "english") {

document.getElementById("title").innerHTML = "My Website - Home";

} else if (userLanguage === "spanish") {

document.getElementById("title").innerHTML = "Mi Sitio Web - Inicio";

} else if (userLanguage === "chinese") {

document.getElementById("title").innerHTML = "我的网站 - 主页";



In this example, we are using conditional statements to change the <title> element based on the user's language.

In conclusion, changing the <title> element with JavaScript can greatly enhance the user experience and add functionality to your website. It allows for dynamic and personalized titles, making it easier for users to navigate and remember your website. With the help of event listeners and conditional statements, we can easily update the <title> element to fit any scenario. So don't be afraid to get creative and experiment with changing the <title> element in your next web project.

Related Articles

Autosizing Textareas with Prototype

Textareas are a fundamental element in web development, allowing users to input and edit large amounts of text. However, as the size of the ...

btaining the Height of a Table Row

When designing a website, it is important to pay attention to the layout and formatting of your content. One crucial element in creating a w...