Modifying Content In JavaScript

Javascript 8 min min read Updated: Mar 09, 2026 Intermediate
Modifying Content In JavaScript
Intermediate Topic 3 of 15

One of the most powerful features of JavaScript is the ability to modify the content of a webpage dynamically. By using the DOM (Document Object Model), JavaScript can change text, update HTML elements, and modify webpage content without reloading the page.

This capability is widely used in modern web applications to update user interfaces, display dynamic data, and respond to user actions instantly.

Why Modifying Content is Important

When users interact with a webpage, the content often needs to change. For example, a website may update messages, display notifications, or show results after form submission.

Key Point: JavaScript can update webpage content instantly without refreshing the page.

Using innerHTML

The innerHTML property allows developers to change the HTML content inside an element. It can insert text, HTML tags, or other elements dynamically.

javascript document.getElementById("message").innerHTML = "Welcome to JavaScript DOM Tutorial";
Output

The content inside the selected element changes to "Welcome to JavaScript DOM Tutorial"

Using textContent

The textContent property is used to change only the text inside an element. Unlike innerHTML, it does not interpret HTML tags.

javascript document.getElementById("title").textContent = "Learning JavaScript DOM";
Output

The text inside the selected element changes to "Learning JavaScript DOM"

Key Point: textContent is safer than innerHTML because it prevents HTML injection.

Using innerText

The innerText property also updates the visible text inside an element. It only affects text that is visible on the webpage.

javascript document.getElementById("info").innerText = "Content updated successfully";
Output

The visible text inside the element changes to "Content updated successfully"

Updating Multiple Elements

JavaScript can also modify multiple elements at once by selecting a group of elements and updating their content.

javascript let items = document.getElementsByClassName("menu-item"); items[0].textContent = "Home"; items[1].textContent = "About"; items[2].textContent = "Contact";
Output

The text of menu items updates dynamically

Difference Between innerHTML, innerText, and textContent

  • innerHTML – Changes HTML content including tags
  • innerText – Changes only visible text
  • textContent – Changes all text inside the element
Key Point: textContent is usually preferred when updating plain text because it is faster and more secure.

Conclusion

Modifying content with JavaScript allows developers to create dynamic and interactive webpages. By using properties such as innerHTML, innerText, and textContent, developers can update webpage content instantly.

These techniques are widely used in modern web development to display data, update messages, and create responsive user interfaces.

In the next tutorial, you will learn about Changing Styles with JavaScript, which explains how JavaScript can dynamically modify CSS styles of HTML elements.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators