JSFiddle - React, Tailwind, and code Playground
HTML
<div class="button" data-target="#text" data-shown-text="Hide" data-hidden-text="Show">Hide</div>
<div id="text"> I want to hide this by pressing the button above </div>
CSS
.button {
display: block;
height: 20px;
width: 100px;
background: lightgreen;
text-align: center;
padding: 5px;
font: 15px Tahoma;
border: 1px solid black;
text-decoration: none;
list-style:none;
margin: 10px 0px 10px 0px;
}
#showhide {
display: none;
}
JavaScript
var button = document.querySelector('.button');
button.addEventListener('click', function(event) {
var target = document.querySelector(button.getAttribute('data-target'));
if (target.style.display == "none") {
target.style.display = "block";
button.innerHTML = button.getAttribute('data-shown-text');
} else {
target.style.display = "none";
button.innerHTML = button.getAttribute('data-hidden-text');
}
});