How can I include div 3 and 4 and 5 and 6 and so on ?
I want to overwrite multiple content items with a button / switch
content id 1 and 3 is always visible and changes after clicking / swtich with the buttons
function openOne(oneName) {
var i;
var x = document.getElementsByClassName("one");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
document.getElementById(oneName).style.display = "block";
}
<div id="1" class="one">
<p>Content 1</p>
<p>Content 2</p>
</div>
<div id="2" class="one" style="display:none">
<p>replaced Content 1</p>
<p>replaced Content 2</p>
</div>
<div id="3" class="one">
<p>Content 3</p>
<p>Content 4</p>
</div>
<div id="4" class="one" style="display:none">
<p>replaced Content 3</p>
<p>replaced Content 4</p>
</div>
<div id="myDIV">
<button class="bttn activ" onclick="openOne('1')">Content</button>
<button class="bttn" onclick="openOne('2')">replaced Content</button>
</div>
2
Answers
It’s not entirely clear what you want but my best interpretation is that you want to be able to toggle multiple elements like so:
Maybe you should move away from the
id
based approach, as this does not scale very well when you want to add further divs.I based the following snippet on the classes
one
andtwo
: