I know it should be simple, but I couldn't find a way to do it and I have a feeling that is simple but I am stuck. I am trying to display a message every time a checkbox is checked. I have the code below but I can only display the message for the first checkbox. I am just starting with Javascript.
function myFunction() {
var checkBox = document.getElementById("myCheck");
var text = document.getElementById("text");
if (checkBox.checked == true){
text.style.display = "block";
} else {
text.style.display = "none";
}
}
<!DOCTYPE html>
<html>
<body>
<p>Display some text when the checkbox is checked:</p>
Checkbox: <input type="checkbox" id="myCheck" onclick="myFunction()">
<p id="text" style="display:none">Checkbox is CHECKED!</p>
Checkbox2: <input type="checkbox" id="myCheck" onclick="myFunction()">
<p id="text" style="display:none">Second checkbox is checked</p>
Checkbox3: <input type="checkbox" id="myCheck" onclick="myFunction()">
<p id="text" style="display:none">Third checkbox is checked</p>
</body>
</html>