Please help! I have made the simple two-check box to change the text color on the same page but the result works with only one check box (left-box) the (right-box) is not working. if I comment the (left-box) of JavaScript the (right-box) work normally
Thank you for your help
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Example</title>
</head>
<body>
<div class="container">
<div class="left-box">
<div class="checkbox">
<input type="checkbox" name="checkbox" id="checkbox1" />
</div>
<div class="display-text">
<h1 id="greetingL">Hello</h1>
</div>
</div>
<div class="right-box">
<div class="checkbox">
<input type="checkbox" name="checkbox" id="checkbox2" />
</div>
<div class="display-text">
<h1 id="greetingR">Hello</h1>
</div>
</div>
</div>
<script src="./checkboxleft.js"></script>
<script src="./checkboxright.js"></script>
</body>
</html>
JavaScript for the Left-Box
const leftcheckbox = document.getElementById("checkbox1");
const greetingtex = document.getElementById("greetingL");
leftcheckbox.addEventListener("change", () => {
if (leftcheckbox.checked) {
greetingtex.style.color = "Red";
} else {
greetingtex.style.color = "";
}
});
JavaScript for the Right-Box
const rightcheckbox = document.getElementById("checkbox2");
const greetingtex = document.getElementById("greetingR");
rightcheckbox.addEventListener("change", () => {
if (rightcheckbox.checked) {
greetingtex.style.color = "Red";
} else {
greetingtex.style.color = "";
}
});