Careeroza
Changing Content & Styles
Content and Attributes
Example
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h2 id="title">Old title</h2>
<a id="link" href="#">A link</a>
<script>
const title = document.getElementById("title");
title.textContent = "New title"; // plain text (safe)
const link = document.getElementById("link");
link.href = "https://developer.mozilla.org";
link.setAttribute("target", "_blank");
link.innerHTML = "<b>MDN</b> docs"; // parses HTML: never with user input
</script>
</body>
</html>Result
Styles and Classes
Example
Prefer toggling classes over setting many inline styles:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
.highlight { background: #04AA6D; color: white; padding: 6px; }
</style>
</head>
<body>
<p id="msg">Watch me change.</p>
<button id="btn">Toggle highlight</button>
<script>
const msg = document.getElementById("msg");
msg.style.fontSize = "20px";
document.getElementById("btn").addEventListener("click", () => {
msg.classList.toggle("highlight");
});
</script>
</body>
</html>Result