I'm a freelance user experience consultant who delivers projects large and small for a client base that includes multinationals, SMEs, and sole traders. I specialise in formulating new media strategy, defining information architecture, user experience design, interaction design, prototyping, user research & testing, website and application building, and online marketing.

How to create hide/show FAQs in Squarespace

Here's a concise solution for creating hide/show text in Squarespace pages, using a Markdown Block and a jQuery script.

The code on this page is free for you to install on your Squarespace site.

+ How does it work?

The solution uses a Markdown block to hold the questions and answers. For this version of the script it should be the only Markdown block on the page, though I may improve the script at a later date to allow for multiple open/close areas.

A jQuery script then manipulates the Markdown block to add the open/close behaviour.

+ Why Markdown?

The native Squarespace html layouts can be difficult to interpret. Placing a single Markdown block on a page makes it very easy to target with scripting as it has a class of .markdown-block.

Markdown is also easier for a non technical user to edit or add content compared to raw html.

It supports bullet points and otherstylingcodes

Markdown blocks can also include html, which opens up the possibility for richer experiences that include imagery and other embedded media.

+ Why jQuery

jQuery is a Javascript framework that removes a lot of complexity when coding solutions that need to work on multiple web browsers.

This open/close functionality is expressed in 4 lines of jQuery code (excluding the call to the main jQuery library) whereas a pure Javascript solution would be far longer.

It's true that the jQuery library can add page load time, but in my code I link to the one hosted on Google's content delivery network, which means there's a strong chance that a user already has the jQuery library in their browser cache.

<script>// Document ready tells the script to fire only once the entire page is loaded. $(document).ready(function(){
// The next line tells the mouse cursor to becom a pointer when hovering over the H2 elements in the Markdown. $('.markdown-block .sqs-block-content h2').css('cursor','pointer');
// The next line initialises the open/close areas by hiding the content $(".markdown-block .sqs-block-content h2").nextUntil("h2").slideToggle();
//Finally, there is a function that listens for a click on an H2 and either shows or hides its content depending on its current state. $(".markdown-block .sqs-block-content h2").click(function() {$(this).nextUntil("h2").slideToggle();});
});
</script>

Variation 1 - Making displayed answer retract when a new FAQ is clicked: