Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Modifying Text and HTML | Modifying and Styling the DOM
DOM Manipulation with JavaScript

bookModifying Text and HTML

To change what users see on a web page, you often need to update the content of elements dynamically using JavaScript. There are three main properties for modifying an element's content: textContent, innerText, and innerHTML. Each serves a different purpose and behaves differently depending on your needs.

The textContent Property

Gets or sets all the text inside an element, including text in hidden elements, and ignores any HTML tags. This is useful when you want to insert or retrieve plain text without any formatting or markup.

The innerText Property

Also gets or sets the text inside an element, but it takes CSS styles into account. It will not return the text of hidden elements, and it may format whitespace to reflect how the text would actually appear to the user. Use innerText when you want to show or get the visible text as it appears on the page.

The innerHTML Property

Gets or sets the HTML markup contained within an element. This allows you to insert or retrieve not just text, but also tags and other HTML structures. Use innerHTML when you need to add or modify HTML elements inside another element.

index.html

index.html

script.js

script.js

copy
question mark

Which property should you use if you want to insert HTML tags (like <span> or <strong>) into an element's content?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you give examples of when to use each property?

What are the security risks of using innerHTML?

How do these properties affect performance?

Awesome!

Completion rate improved to 6.67

bookModifying Text and HTML

Swipe to show menu

To change what users see on a web page, you often need to update the content of elements dynamically using JavaScript. There are three main properties for modifying an element's content: textContent, innerText, and innerHTML. Each serves a different purpose and behaves differently depending on your needs.

The textContent Property

Gets or sets all the text inside an element, including text in hidden elements, and ignores any HTML tags. This is useful when you want to insert or retrieve plain text without any formatting or markup.

The innerText Property

Also gets or sets the text inside an element, but it takes CSS styles into account. It will not return the text of hidden elements, and it may format whitespace to reflect how the text would actually appear to the user. Use innerText when you want to show or get the visible text as it appears on the page.

The innerHTML Property

Gets or sets the HTML markup contained within an element. This allows you to insert or retrieve not just text, but also tags and other HTML structures. Use innerHTML when you need to add or modify HTML elements inside another element.

index.html

index.html

script.js

script.js

copy
question mark

Which property should you use if you want to insert HTML tags (like <span> or <strong>) into an element's content?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt