Borislav Hadzhiev
Tue Jan 04 2022·3 min read
Photo by Ben White
Use the textContent
property to change the text of a span
element, e.g.
span.textContent = 'Replacement text'
. The textContent
property will set the
text of the span
to the provided string, replacing any of the existing
content.
Here is the HTML for the examples in this article.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <span id="span">Initial span text</span> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const span = document.getElementById('span'); // ✅ Change (replace) the text of the span span.textContent = 'Replacement span text'; // ✅ Change (replace) the content with HTML span.innerHTML = `<span style="background-color: coral">Replacement HTML</span>`; // ✅ Append / Prepend text to the span span.insertAdjacentText('beforeend', ' appended text'); // ✅ Append / Prepend HTML to the span span.insertAdjacentHTML( 'beforeend', `<span style="background-color: salmon"> appended HTML</code>`, );
We used the
textContent
property on the span
to change its text content.
The textContent
property can also be used to read the text content of the
element and its descendants.
textContent
on an element, removes all of the element's children and replaces them with a single text node with the provided string.If you need to completely replace the HTML content of the span
element, use
the
innerHTML
property.
const span = document.getElementById('span'); // ✅ Change (replace) the content with HTML span.innerHTML = `<span style="background-color: coral">Replacement HTML</span>`;
This can be set to any HTML or text your use case requires.
The innerHTML
property gets or sets the HTML contained within the element.
By setting the property on an element, you effectively replace the HTML
previously contained in the span
.
If you need to append / prepend text to the existing content of the span
element, use the insertAdjacentText
method instead.
const span = document.getElementById('span'); // ✅ Append / Prepend text to the span span.insertAdjacentText('beforeend', ' appended text');
The insertAdjacentText method takes the following 2 parameters:
position
- the position relative to the element of where the text should be
inserted. Can be one of the following 4:beforebegin
- before the element itself.afterbegin
- just inside the element, before its first child.beforeend
- just inside the element, after its last child.afterend
- after the element itself.data
- the string from which to create a new text node to insert at the
given position.span
element, before its last child, but you can change the value of the position
parameter depending on your use case.If you need to insert HTML to the span
element, use the
insertAdjacentHTML
method.
const span = document.getElementById('span'); // ✅ Append / Prepend HTML to the span span.insertAdjacentHTML( 'beforeend', `<span style="background-color: salmon"> appended HTML</code>`, );
The first parameter the insertAdjacentHTML
method takes is the same as
insertAdjacentText
- the position at which the HTML should be inserted.
The second parameter is an HTML string containing the content you want to insert.