Hi. I made a little accordion FAQ that pulls the text from a google spreadsheet with the help of @MarkHunte script. However all the panels are open by default, and I need to have them all closed at the start and only open the one that is clicked on (closing the previously opened if there is an open instance). Secondly I have only been able to make this work by initiating the javascript onMouseClick, of the Accordion container, but it only seems to work on doubleclick to open/close the panels. Any help to make this a little smoother would be greatly appreciated. I’m testing Google Sheets as a kind of content management solution.
Thank you.GoogleSheetAccordion.zip (101.7 KB)
This pulls the spreadsheet data:
function GetSheet(hypeDocument, element, event){
$.getJSON("https://spreadsheets.google.com/feeds/list/1qSJs6inNb8oCt-yP-mHLqqGZrLLwWRfyDsbkBH2bT4s/od6/public/values?alt=json", function(data) {
var Accordion = document.getElementById('accordion');
var items = [];
for (i = 0; i < data.feed.entry.length; i++) {
var Question = data.feed.entry[i].gsx$question.$t
var Answer = data.feed.entry[i].gsx$answer.$t
items.push( '<button class="accordion">' + Question + '</button><div class="panel"><p>' + Answer + '</p></div>');
Accordion.innerHTML = '<div>' + items + '</div>';
}
});
This does the Accordion part
function Accordion(hypeDocument, element, event){
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active");
/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
}
This does the CSS part
<style>
/* Style the buttons that are used to open and close the accordion panel */
.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;
}
/* 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) */
.active, .accordion:hover {
background-color: #ccc;
}
/* Style the accordion panel. Note: hidden by default */
.panel {
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;
}
</style>