I using this script to show/hide content. However, how can I close open elements when opening one?
I tried with something like this, but it’s not working correctly:
// Get siblings
var contentSiblings = document.querySelectorAll('.content');
// close them before opening new one
if (contentSiblings) {
contentSiblings.forEach((elem) => {
if (elem.classList.contains('is-visible')) {
hide(elem);
}
});
}
// Show/hide filter content
var show = function (elem) {
elem.classList.add('is-visible');
};
// Hide an element
var hide = function (elem) {
elem.classList.remove('is-visible');
};
// Toggle element visibility
var toggle = function (elem) {
// If the element is visible, hide it
if (elem.classList.contains('is-visible')) {
hide(elem);
return;
}
// Otherwise, show it
show(elem);
};
// Listen for click events
document.addEventListener('click', function (event) {
// Make sure clicked element is our toggle
if (!event.target.classList.contains('btn')) return;
// Prevent default link behavior
event.preventDefault();
// Get the selected content
var content = document.querySelector(event.target.hash);
if (!content) return;
// Toggle the content
toggle(content);
}, false);
.content {
display: none;
opacity: 0;
transition: opacity .15s linear;
}
.content.is-visible {
display: block;
opacity: 1;
}
<a class="btn" id="btn-one" href="#one">Button one</a>
<a class="btn" id="btn-two" href="#two">Button two</a>
<div class="content" id="one">Content one</div>
<div class="content" id="two">Content two</div>
3
Answers
Probably a simple approach would just be to always hide all "content" elements before showing one. For example, the
show
function can first hide all elements and then show the specified element:You can add a function
hideAllElements
to hide all visible elements before show content:On a side note, if you give descriptive names to your functions, the comments are not needed anymore.
you can also do: