Blog>
Snippets

Select and Replace Text Content

Select an element by its ID `info-text` and update its text content.
// Select the element with ID 'info-text'
var infoText = document.getElementById('info-text');

// Update the text content of the selected element
infoText.textContent = 'New text content goes here';
This code first retrieves the DOM element with the ID 'info-text'. Then, it sets its text content to a new string 'New text content goes here'.