How to call same onclick function on multiple element? I want to make show text and show div onclick function as a single click.
I want to call a onclick function that opens div and changes backgoundcolor and fontcolor when I click on any of parts.
Now onclick function only works on first element. Please help.
function myFunction() {
document.getElementById("answer").style.color = "red";
document.getElementById("answer").style.backgroundColor = "white";
var x = document.getElementById("explain");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
<div class="test">
<ul class="example">
<li>This is</li>
<li id="answer" onclick="myFunction()" style="background-color:#444444; color:#444444;">example.
</li>
</ul>
<ul id="explain" style="display:none">
TEST1
</ul>
</div>
<div class="test">
<ul class="example">
<li>This is</li>
<li id="answer" onclick="myFunction()" style="background-color:#444444; color:#444444;">example2.
</li>
</ul>
<ul id="explain" style="display:none">
TEST2
</ul>
</div>
<div class="test">...</div> X Loop