My idea was to create a button to hide/show all hidden text (Hide by highlighting the text in black, show by making the background color of the text transparent). But the codes, as shown below, only works for the 1st element with the given ID ("p2").
Javascript:
function change()
{
var test = document.getElementById("button1");
if (test.value=="Hide Spoiler")
{
test.value = "Open Spoiler";
document.getElementById("p2").style.backgroundColor="black";
}
else
{
test.value = "Hide Spoiler";
document.getElementById("p2").style.color="black";
document.getElementById("p2").style.backgroundColor="transparent";
}
}
HTML:
<input onclick="change()" type="button" value="Open Curtain" id="button1"></input>
<br>
<span id="p2">Hidden text</span> Test for more <span id="p2">Hidden text</span> test again. <span id="p2">Hidden text</span>
How would I make all the elements with similar ID to change its CSS style using only 1 button? Thanks.