How do you select and modify an element's text content?
Short answer
Use document.querySelector() (or similar) to find the element, then set its textContent property to update the text safely without interpreting it as HTML.
Advertisement
AModel answer
Use document.querySelector() (or similar) to find the element, then set its textContent property to update the text safely without interpreting it as HTML.
const el = document.querySelector('#status');
el.textContent = 'Saved successfully';Real-world example
Updating a status message on screen after a save action completes.
Likely follow-ups
- What's the security risk of using innerHTML instead of textContent with user input?
Related topics