<html>
<head>
<meta charset="UTF-8">
<title>Basic DOM Manipulation</title>
</head>
<body>
<main>
<button class="colored_button">
I'm colored red
</button>
</main>
</body>
</html>
main{
display: flex;
align-items:center;
justify-content:center;
padding-top:3rem;
}
.colored_button {
border-radius: 5px;
width: max-content;
height: 3rem;
color: white;
font-size: 2rem;
background-color: red;
cursor: pointer;
}
const button = document.querySelector(".colored_button");
let current = true;
button.addEventListener("click", function (e) {
if (current) {
button.textContent = "I'm colored blue";
button.style.backgroundColor = "blue";
current = !current;
} else {
button.textContent = "I'm colored red";
button.style.backgroundColor = "red";
current = !current;
}
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.