Recent searches


No recent searches

Add Accordion based on Table Of Content

Answered


image avatar

Ifra Saqlain

Zendesk LuminaryMost Engaged Community Member - 2022Most Engaged Community Member of The Year - 2021Community Moderator

Posted Jun 18, 2024

Hi Community,

 

This functionality is based on TOC.  After clicking the TOC link, the accordion will open concerning that TOC link.

First, add TOC  for your article page headings and then use the given code for this functionality.

Go through this post to add TOC: 

https://support.zendesk.com/hc/en-us/community/posts/4408867942298-Tip-How-to-make-a-Table-Of-Contents-TOC-For-Article-Template

 

 

Follow the below steps to get the open accordion panel after clicking on TOC link:-

 

 I). Add HTML code to your template.

 <h2 class="accordion">Section One</h2>
<div id="panel-one" class="panel" style="">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<h2 class="accordion">Section Two</h2>
<div id="panel-two" class="panel">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<h2 class="accordion active">Section Three</h2>
<div id="panel-three" class="panel" style="">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<h2 class="accordion">Section Four</h2>
<div id="panel-four" class="panel">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>

 

 Add HTML code to your ‘Source code’  while creating the article to show accordion functionality on your article page.

 

 

II). Add JQuery CDN to your document_head.hbs file.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

 

III). Add JS code to your script.js file inside the TOC code block, please see the given screenshot.

var id = 1;
$('.panel').each(function(){
    $(this).attr("id", "panel_" + id);
    id++;
})
  
   $("#toc > li:nth-child(1)").click(function() {
    $('#panel_1').toggleClass("toggleClass");
  })
  
  
  $("#toc > li:nth-child(2)").click(function() {
    $('#panel_2').toggleClass("toggleClass");
  })
  
    $("#toc > li:nth-child(3)").click(function() {
    $('#panel_3').toggleClass("toggleClass");
  })
  
    $("#toc > li:nth-child(4)").click(function() {
    $('#panel_4').toggleClass("toggleClass");
  })
  
  
  
  

 

 

Go to the article_page.hbs file and add this given code just after the {{article.body}}, this code is for ‘return to top’ , you won't need to scroll via mouse, click on it. Please see the given screenshot for the same.

 

 <div class="article-return-to-top show_always">
         <a href="#article-container">
           {{t 'return_to_top'}}
           <svg xmlns="http://www.w3.org/2000/svg" class="article-return-to-top-icon" width="20" height="20" focusable="false" viewBox="0 0 12 12" aria-hidden="true">
             <path fill="none" stroke="currentColor" stroke-linecap="round" d="M3 4.5l2.6 2.6c.2.2.5.2.7 0L9 4.5"/>
           </svg>
         </a>
       </div>

 

IV). Add the below code at the bottom of your style.css file.

html {
  scroll-behavior: smooth;
}

.accordion {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  padding: 12px;
  width: 100%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
  margin-bottom: 10px;
}

.accordion p {
  display: inline;
}

.active, .accordion:hover {
  background-color: #ccc;
}

.panel {
  padding: 0 18px;
  background-color: white;
  max-height: 0;
  overflow: hidden;
}

.toggleClass {max-height:100%;}

.article-return-to-top.show_always {
  display: block;
}

 

OUTPUT is:

 

 

If any issues fee free toask :)

 

Thank You


1

6

6 comments

image avatar

Brett Bowser

Zendesk Community Manager

Awesome user tip Ifra Saqlain ! Thanks for writing this up for everyone :) 

1


image avatar

Ifra Saqlain

Zendesk LuminaryMost Engaged Community Member - 2022Most Engaged Community Member of The Year - 2021Community Moderator

Thank you so much, Mr. Brett :)

0


Hi Ifra, 
Unfortunately, I can't get it to work. We already have code for the accordion function. I hope that doesn't matter. 
I have slightly adapted my source code to our code (changed the class to our class), but unfortunately the toggleClass and the panel do not work. What do I need to check? See attached some screenshots (yellow marked is the “old” code).

Sourcecode: <p>
 <a href="/hc/de/articles/26343147679377#h_01J1PWJHAKKBDMR132AE75AH86">Section One</a>
</p>
<p>
 <a href="/hc/de/articles/26343147679377#h_01J1PWJHAKW1F9RA169YDP3XWE">Section Two</a>
</p>
<p>
 <a href="/hc/de/articles/26343147679377#h_01J1PWJHAKC8J1CMPQDA9HTT9E">Section Three</a>
</p>
<p>
 <a href="/hc/de/articles/26343147679377#h_01J1PWJHAKHXCRRXKRPAS8FYWE">Section Four</a>
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3 id="h_01J1PWJHAKKBDMR132AE75AH86" class="article-accordion">Section One</h3>
<div id="panel-one" class="panel">
 <p>
   Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
   quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
 </p>
</div>
<h3 id="h_01J1PWJHAKW1F9RA169YDP3XWE" class="article-accordion">Section Two</h3>
<div id="panel-two" class="panel">
 <p>
   Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
   quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
 </p>
</div>
<h3 id="h_01J1PWJHAKC8J1CMPQDA9HTT9E" class="article-accordion">Section Three</h3>
<div id="panel-three" class="panel">
 <p>
   Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
   quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
 </p>
</div>
<h3 id="h_01J1PWJHAKHXCRRXKRPAS8FYWE" class="article-accordion">Section Four</h3>
<div id="panel-four" class="panel">
 <p>
   Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
   quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
 </p>
</div>

0


image avatar

Ifra Saqlain

Zendesk LuminaryMost Engaged Community Member - 2022Most Engaged Community Member of The Year - 2021Community Moderator

Hi Ronja, 

I updated this post, please go through it again :)

Remove your previous accordion code and add only this given code.

 

Thanks

0


Hi Ifra, 

 

In this case, I can open/close the accordions by clicking the TOC but cannot do so by clicking the headers. 

Is there any way that both methods are possible to open/close the accordions? 

 

( When I use Step 1 code in this article, and Step 2 code and Step 3 code in Tip: Collapsible headers in articles or templates (accordions) (https://support.zendesk.com/hc/en-us/community/posts/4408860552730)

It works. 

In this case, the source code is changing to following.

<h2 id="h_01J2ZPZRCJ8WC0MGT7S9WA0E32" class="accordion">Section One</h2>
<div id="panel-one" class="panel">

It has both h2 id and div id. Does it need two id?

I want to simplify the code. 

)

 

Thanks.

0


image avatar

Ifra Saqlain

Zendesk LuminaryMost Engaged Community Member - 2022Most Engaged Community Member of The Year - 2021Community Moderator

 Hi Kidam Mun, it's possible but you need to customize the JS code.

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post