var sections = ["intro", "content"];
var visibleSectionId = null;
function toggleVisibility(sectionId) {
if (visibleSectionId == sectionId) {
visibleSectionId = null;
} else {
visibleSectionId = sectionId;
}
hideInvisibleSections();
}
function hideInvisibleSections() {
var i, sectionId, section;
for (i = 0; i < sections.length; i++)
{
sectionId = sections[i];
section = "#" + document.getElementById(sectionId).id;
if (visibleSectionId == sectionId)
$(section).fadeIn();
else
$(section).fadeOut();
}
}
Initially, it works correctly. However, when attempting to re-open the same section after closing it by clicking on an element, it requires two clicks. This behavior is due to the functionality where the visibleSectionId is reset to null upon first click and then fadeOut occurs on the second click since it no longer matches the selected section id.
I understand the issue at hand, but I am unsure about how to address it effectively.