When you click on a certain button, a certain text is displayed, and when you click on another button, the text changes to another one. The problem is that I need to indicate all possible identifiers of texts that need to be hidden. How to specify only the id of the appearing text, without specifying the ids of the hiding texts?
I don’t know English well, so I used Google translator.
function hideh1(Id){
document.getElementById(Id).style.display = "none";
}
function showh1(Id){
document.getElementById(Id).style.display = "block";
}
<button onclick="showh1('Text11'); hideh1('Text00');
hideh1('Text22'); hideh1('Text33')">Text1</button>
<button onclick="showh1('Text22'); hideh1('Text00');
hideh1('Text11'); hideh1('Text33')">Text2</button>
<button onclick="showh1('Text33'); hideh1('Text00');
hideh1('Text11'); hideh1('Text22')">Text3</button>
<button onclick="showh1('Text00'); hideh1('Text11');
hideh1('Text22'); hideh1('Text33')">Text00</button>
<h1 id="Text00">Text00</h1>
<h1 id="Text11" style="display:none">Text11</h1>
<h1 id="Text22" style="display:none">Text22</h1>
<h1 id="Text33" style="display:none">Text33</h1>
2
Answers
You can hide all
h1
elements or, you can hide all elements that have an ID that starts withText
or, you can give all elements a class name and then hide all elements that have that class name.Here is how the last approach that I’ve mentioned looks like: