JavaScript › DOM & Events

How do you select and modify an element's text content?

Beginner

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.

Common follow-ups: What's the security risk of using innerHTML instead of textContent with user input?