Categories
JavaScript Answers

How to change the text of a span element using JavaScript?

Spread the love

Sometimes, we want to change the text of a span element using JavaScript.

In this article, we’ll look at how to change the text of a span element using JavaScript.

How to change the text of a span element using JavaScript?

To change the text of a span element using JavaScript, we change its textContent property.

For instance, we write

document.getElementById("myspan").textContent = "new text";

to select the span with getElementById.

Then we set its textContent property to change the text.

Conclusion

To change the text of a span element using JavaScript, we change its textContent property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *