My go-to source for codes is usually https://www.w3schools.com. They also provide an accordion feature along with the code;
However, I've encountered an issue where when a link is used -> to The accordion doesn't open but remains closed. Does anyone know how to make the accordion open when linking to content inside it?
Thank you.
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
}
}
@charset "utf-8";
/* CSS Document */
/* Style the buttons that are used to open and close the accordion panel */
button.accordion {
background-color: #530010;
color: #FFF;
font-weight: bolder;
cursor: pointer;
padding: 10px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;
border-radius: 5px;
}
/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
button.accordion.active, button.accordion:hover {
background-color: #a20121;
}
/* Style the accordion panel. Note: hidden by default */
div.panel {
padding: 0px 5px;
margin-bottom: 5px;
background-color: #none;
max-height: 0px;
overflow: hidden;
transition: max-height 0.2s ease-out;
}
button.accordion:after {
content: '\02795'; /* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #FFF;
font-weight: bolder;
float: right;
margin-left: 5px;
}
button.accordion.active:after {
content: "\2796"; /* Unicode character for "minus" sign (-) */
}
<a href="schedule.html#0906">2018.09.06</a>(木) 今池GROW<br><br>
<button class="accordion"><a name="0906">■</a>2018/09/06(木)今池GROW </button>
<div class="panel">
<p>
Testing
</p>
</div>