As a newcomer to JavaScript, I am eager to learn how to use it to change the background color of an element by altering its class. Despite combining code snippets from various sources, my attempt is not yielding the desired results and the reason eludes me.
function myFunc() {
var y = document.getElementById("bg-change1").getAttribute("class");
if (y === "normal") {
y = "active";
} else {
y = "normal";
}
}
.normal {
background-color: white;
}
.active {
background-color: green;
}
<body>
<button onclick="myFunc()">click here</button>
<div id="bg-change1" class="normal">
<p>Lorem ipsum and etc</p>
</div>
</body>