← CodePrimer
JavaScript Beginner Dom

innerHTML vs textContent

How do I change an element’s text?

Safe text vs HTML injection.

Sample
const el = document.querySelector('#status');
el.textContent = 'Saved successfully'; // safe plain text
// el.innerHTML = '<strong>Saved</strong>'; // parses HTML — careful with user data

Notes

Prefer textContent for user-provided strings (XSS safety).

Keywords: innerHTML · textContent · dom · xss

Related

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share