You can also achieve the same effect using data-id on the button and showing the div that associates with that.
See this answer for more ways to highlight for the copying part
let currentID = 1;
document.getElementById("options").addEventListener("click", e => {
if(e.target.dataset.id) {
document.querySelectorAll(".tabs-content div").forEach(tab => {
tab.style.display = "none";
})
document.querySelector(`.tabs-content [data-id="${e.target.dataset.id}"]`).style.display = "block";
currentID = e.target.dataset.id;
} else {
const currentNode = document.querySelector(`.tabs-content [data-id="${currentID}"]`);
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(currentNode);
selection.removeAllRanges();
selection.addRange(range);
document.execCommand("copy");
selection.removeAllRanges();
}
});
#options {
display: flex;
background-color: black;
color: white;
padding: 15px;
justify-content: space-around;
}
#options span {
fill: white;
width: 10px;
}
.tabs-content {
box-sizing: border-box;
width: 100%;
background-color: darkblue;
color: white;
padding-left: 30px;
}
<div>
<nav id="options">
<a data-id="1">First</a>
<a data-id="2">Second</a>
<a data-id="3">Third</a>
<a data-id="4">Fourth</a>
<span>
<svg height="15" viewBox="0 0 16 16" width="15" xmlns="http://www.w3.org/2000/svg" class="copy-btn">
<path d="m7 5h2c1.6568542 0 3-1.34314575 3-3 1.1045695 0 2 .8954305 2 2v10c0 1.1045695-.8954305 2-2 2h-8c-1.1045695 0-2-.8954305-2-2v-10c0-1.1045695.8954305-2 2-2 0 1.65685425 1.34314575 3 3 3zm-1-3c0-1.1045695.8954305-2 2-2s2 .8954305 2 2c0 .55228475-.44771525 1-1 1h-2c-.55228475 0-1-.44771525-1-1z" fill-rule="evenodd"></path>
</svg>
</span>
</nav>
</div>
<div class="tabs-content" style="float: left;">
<div class="code" data-id="1">
<p>This is the first div</p>
</div>
<div class="code" data-id="2" style="display: none;">
<p>This is the second div</p>
</div>
<div class="code" data-id="3" style="display: none;">
<p>This is the third div</p>
</div>
<div class="code" data-id="4" style="display: none;">
<p>This is the fourth div</p>
</div>
</div>