Hello everyone!
I was struggling with this as well initially and read through this entire thread trying to figure it out. Gave up and went my own way. Here is the code and instructions if anyone else needs it:
Here's a step-by-step guide:
-
Access Your Unbounce Page:
- Log in to your Unbounce account and open the landing page where you want to add the accordion FAQ.
-
Insert a Custom HTML Widget:
- In the Unbounce editor, drag a Custom HTML widget onto your page and area where you want the FAQ section.
- Click on the widget to edit it.(It should pop open automatically)
-
Add the HTML and CSS for the Accordion:
HTML:
Replace the “Question 1” etc text with a question, and the “Answer 1” with the answer. Continue for more questions/answers.
<div class="accordion">
<div class="accordion-item">
<button id="accordion-button-1" aria-expanded="false"><span class="accordion-title">Question 1</span></button>
<div class="accordion-content">
<p>Answer 1</p>
</div>
</div>
<div class="accordion-item">
<button id="accordion-button-2" aria-expanded="false"><span class="accordion-title">Question 2</span></button>
<div class="accordion-content">
<p>Answer 2</p>
</div>
</div>
<div class="accordion-item">
<button id="accordion-button-3" aria-expanded="false"><span class="accordion-title">Question 3</span></button>
<div class="accordion-content">
<p>Answer 3</p>
</div>
</div>
</div>
CSS(And some basic CSS to style it):
<style>.accordion {
/* Style for accordion container */
}
.accordion-item {
/* Style for each item */
}
.accordion-content {
display: none;
/* Style for the content */
}
</style>
Add JavaScript for Interactivity:
<script>
document.querySelectorAll('.accordion button').forEach(button => {
button.addEventListener('click', () => {
const accordionContent = button.nextElementSibling;
button.setAttribute('aria-expanded', button.getAttribute('aria-expanded') === 'false' ? 'true' : 'false');
accordionContent.style.display = accordionContent.style.display === 'block' ? 'none' : 'block';
});
});
</script>
For Javascript:
Ensure that the Javascript goes under the Javascript section. You must also include the <script></script> tags in Unbounce. “Before body end tag” is also fine for the script.
For Stylesheet:
CSS *must* include the <style></style> as well.
Please note I am a complete noob, but this works perfectly.
Thanks @Michael_McInnes1.
I just want the same collapsible section to work on 2 or more section in same page.
Here is the code:
<script>
//Replace with ID of your collapsible page section
var toggleSection = $('#lp-pom-block-3241');
//Replace with ID of box containing hidden/visible content
var toggleContent = $("#lp-pom-box-3242");
//Replace with ID of button that toggles section
var toggleButton = $("#lp-pom-button-3240");
// Height of section to show/hide
var sectionHeight = $(toggleSection).height();
//Top value of content
var toggleContentTop = toggleContent.position().top;
toggleSection.css("display", "none");
toggleContent.css("display", "none");
var otherSections = toggleSection.nextAll();
var otherContent = toggleContent.siblings();
console.log(otherContent);
var shown = false;
var moveStuff = function(){
if(shown){
for (var i=0;i<otherContent.length;i++){
var content = $(otherContent[i]);
var contentTop = content.position().top;
//Is this element below toggleContent?
if( contentTop > toggleContentTop ){
var newTopValue = contentTop + sectionHeight;
content.animate({top: "+=" + sectionHeight + "px"}, 600);
}
}
$("#lp-pom-root, #lp-pom-root-color-overlay").height(function (index, height) {
return (height - sectionHeight);
});
shown = false;
}else{
for (var i=0;i<otherContent.length;i++){
var content = $(otherContent[i]);
var contentTop = content.position().top;
//Is this element below toggleContent?
if(contentTop > toggleContentTop ){
var newTopValue = content.position().top - sectionHeight;
content.animate({top: "-=" + sectionHeight + "px"}, 600);
}
}
$("#lp-pom-root, #lp-pom-root-color-overlay").height(function (index, height) {
return (height - sectionHeight);
});
shown = true;
}
}
//Run moveStuff to adjust content on load
moveStuff();
toggleButton.click(function() { // ID of button/trigger
toggleSection.slideToggle('slow');
toggleContent.slideToggle('slow');
moveStuff();
});
/**
* Do not remove this section; it allows our team to troubleshoot and track feature adoption.
* TS:0002-03-059
*/
</script>
Hey @dipankar27, thanks for posting! This script only works for 1 section per page, when trying to use it across multiple sections on the same page the same functionality is not always guaranteed. We cannot help you modify custom code, however, f you’d like to share your exact setup here with the community then with a bit of luck a community member with a little more coding knowledge than our support team can chime in and help you out! I hope you have a great day ahead @dipankar27 and thank you for being an active member of the community!
https://book.cloudninefertility.com/ivf-center-fertility-hospital-bengaluru/
I was trying to create two collapsible section with two separate code, but it’s not working.
Please help!
Hi @parkmyvan ,
Whilst you are able to alter the button styles (colour, border etc.) natively in the builder, there’s no simple way to change the button text. To achieve that, you’d need to use some custom code to your page. This is a little outside the scope of my ability, although I did find this webpage showing you how this could be achieved using some Javascript.
Sorry I couldn’t be of more help with this, but I hope the above link can assist you in achieving your goal.
Great script! My button design has a + and - as per client request. Is there a way to get the button to go from " + " plus symbol to " - " when it's clicked upon expanding the section? And vice versa for closing it. Thanks in advance!
Thank you! No worries! You all have a wonderful weekend..!
This is awesome! Is there any way to password protect the dropdown box?
Hi Noah - is this still the case today?
Would really love to be able to do multiple sections!
That seems to be something that Unbounce or one of the scripts is throwing - I created a brand new button and got the same results. I wonder if something has gone wonky in this particular page (since it’s working on other pages)? I might have to try re-creating the page from scratch (which is super frustrating). 😦
In this part of your code:
<a class="lp-element lp-pom-button" id="lp-pom-button-783" href><span class="label"><strong>Locations & Providers ▼</strong></span></a>
You have ‘href’, it should not be there. Because this will reload the same page.
I don’t know if you can remove this, maybe you had a link in this element before?
I’m suddenly having an issues where when you click on the button that expands the hidden section, it opens but then quickly closes and moves you back to the top of the page. I thought it might be a conflict of scripts, so I tried stripping out all of them and just using the collapsible section script, but still got the same results.
It works fine in preview mode, but when published it breaks. I’ve compared to other pages I’ve used this on, and everything is the same as those. Any ideas? (page in question: https://online.uncpn.com/)
Hi Gaston,
It’s possible to have 2 FAQ menu on one page. However, the FAQ menus are not interconnected with each other. If the user opens the first item in the first FAQ menu and then scrolls down to the second menu to open an item there. The first item of the first FAQ menu stays open.
This is done by design because otherwise, the user would experience strange behavior in the browser where suddenly the text moves up because the first item of the first accordion closes.
I use this code and works perfect.
Now I have a question. How can I use it multiple times in the same landing? I’ve tried duplicating the code and change all variables (adding a 2 at the end), and the collapse efect works perfect on the first section, but it doesn’t push the second section’s siblings.
Does anyone know why this happens?
Hi, for this functionality I wouldn’t recommend using this script. It’s possible to do it, but it requires a good understanding of HTML and CSS to set it up as you would like.
Hey, I’m thinking about buying this, but want to apply it to large sections chalked full of images/text/colors/etc. Will it work for this application as well? See page here - https://online.homeusa.com/portfolio/
Hi,
My collapsible page section is working, but when I open it the background of my landingpage changes to white in the sections below. However, the overall background of my page is set to black.
Anybody who knows why this happens and how I can solve it? The landingpage is https://www.allroundflandrien.be/ and you can find the collapsible page section if you click on ‘gallerij’ in the navigation and the button ‘Meer foto’s bekijken’ (= show more pictures).
Thank you in advance!
Kind regards,
Martijn
@Peter_Czepiga, select the option ‘Before body End Tag’ for the javascript
Does this javascript get pasted in the Head, Before Body, or After Body?
Hey everyone,
I have been looking for a solution here and in other forums, but none of them gave me the solution I needed.
Most scripts don’t work well with the absolute positioning of the Unbounce sections.
After hours of search and hours of testing, I have created a version that does work very nicely with Unbounce.
SOLUTION
I was thinking to keep it for myself, but then I decided to offer it for a small price for everyone who really needs this feature. Here is the link https://gum.co/tUiUf and use the code ‘earlybird’.
Contact me there if you need any modifications or help with the implementation.
I understand the frustration.
I’ve stopped trying and now i use lightbox.
You create the question you want and you had a transparent button to lightbox (containing only the link) over your question. this way you’re not limited by the design restriction of the button.
Only limitation, is the number of light box (20) and the more you add, the slower the builder will get (at least for me).
lowkey frustrating that this is a script from 2017 that still gets views and comments to this day and most of them are with a problem that no one from Unbounce seems able to adress or help with:
Multiple collapsing sections.
I myself thought this would be a great add for a FAQ yet it did not work, as duplicating the script only overwrite the variables.
I work with C# so i’m not familiar with a lot of syntaxes of javascript. Are there an equivalent of arrays?
These workarounds should be built using whatever is the java equivalent of arrays, so that they can easily scale by letting the user only change the size of the array and dumping in all the needed IDs.
This is the third “workaround” script i’ve found where I wanted to use on multiple elements and wasn’t able to.
Hi, I’m having the same issue…
Someone managed to resolve this?
Thx.
Hola buenas tardes, me gustaria saber como replicar esto y tener varias secciones plegables dentro de mi Landing Page,
les agradeceria mucho su ayuda. 😀