Accordion FAQ populated from Google Sheet

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 (101.7 KB)

This pulls the spreadsheet data:

function GetSheet(hypeDocument, element, event){  		
	$.getJSON("", 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 */

    /* Toggle between hiding and showing the active panel */
    var panel = this.nextElementSibling;
    if ( === "block") { = "none";
    } else { = "block";

This does the CSS part

 		/* 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;

The issue with the answers showing up ahead of time is that a CSS rule from Hype is overriding the head html css. You can fix this by unchecking Protect from external styles in the Document Inspector.

For the double-click issue, the problem is that the Accordion() function only should be run once, and after the sheet has been fetched and HTML setup. So to fix this you’ll want to first remove the On Mouse Click handler that runs that code. Then you can plop a manual call towards the end of the GetSheet() code just after the DOM has been setup in the getJSON callback. Basically below this line:

   Accordion.innerHTML = '<div>' + items + '</div>';



And then I think it will work as expected.

1 Like

Thank you Jonathan! All working as expected… except the toggle. I think I’m getting closer as I can now toggle the state of the headers. But it doesn’t toggle the collapsing of the panel elements? Trying various combinations of removing classes, instead of styling visibility (block/none)? But not much luck. Updated file: (100.4 KB)

var acc = document.getElementsByClassName('accordion')

for (var i = 0; i < acc.length; i++) {
  var elem = acc[i];
  elem.addEventListener('click', function() {
    for (var j = 0; j < acc.length; j++)
  var thispanel = this.nextElementSibling;

1 Like

Searched and found a simple JS on the web. Works really well! The only thing left is to get rid of the pesky commas that separate the lines. (any ideas anyone?)

For anyone interested, here is the final file with a neat working accordion powered by Google Sheets. :grinning: (100.4 KB)

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].onclick = function() {
    var active = document.querySelector("");
    if (active && active != this) {

to avoid the commas in the output :

Accordion.innerHTML = items.join('');

and you should move the above line and setting of innerHTML after the iteration …


Thank you @h_classen that did the trick!