Skip to content
Justice Tech School

Changing Content and Attributes

textContent sets plain text and is safe. innerHTML parses HTML and is dangerous with user input, because it can run malicious code. Use setAttribute, or properties like href and src, to change attributes.

Example
const box = document.getElementById("demo");
box.textContent = "Plain <b>text</b>";
const link = document.createElement("a");
link.textContent = "Visit Example";
link.setAttribute("href", "https://example.com");
document.body.appendChild(link);

Exercise

Set plain, safe text.

el. = userInput;

Quiz Test

Why is innerHTML risky with user input?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.