I have the following code
const collapse = (elem,forceClose) => {
const content = elem.nextElementSibling;
if (!content || !content.matches(".content")) return; // no content after the element, so leave
if (forceClose) elem.classList.remove("active"); // passed force or nothing passed
else elem.classList.toggle("active");
content.style.maxHeight = elem.classList.contains("active") ? content.scrollHeight + "px" : null;
};
const buttons = document.querySelectorAll(".collapsible");
document.getElementById("container").addEventListener("click", e => {
const tgt = e.target;
if (!tgt.matches(".collapsible")) return; // clicked on something not a button
if (tgt.matches(".all")) { // special button
const close = container.querySelectorAll(".collapsible.active").length > 0;
buttons.forEach(but => collapse(but,close));
} else {
collapse(tgt);
}
});
.content {
max-height:0;
overflow:hidden;
}
<div id="container">
<button class="collapsible">XXXXXXXXXX <u>YYYYYYYYYY</u></button>
<div class="content" style="">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
</div>
Click on the XXXXXXXXXX on the button to hide and show the content. This is working as expected.
But if you click on YYYYYYYYYY it's not working, just because YYYYYYYYYY is underlined.
Why doesn't the buttons function not work any more just because of the underlined text? How can I solve this? I need the button to work and the text underlined.