JavaScript Accordion with Arrow

JavaScript Accordion with Arrow
Code Snippet:Accordion Using Pure JavaScript
Author: Abhishek Katiyar
Published: January 17, 2024
Last Updated: January 22, 2024
Downloads: 6,305
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create a responsive accordion with arrow icons. It is based on simple idea to make the content collapsible using class adding method. You can use this accordion for FAQs page or a general-purpose accordion navigation.

Basically, the accordion comes with decent and responsive design. However, the colors, penal size, and overall look and feel can be customized with additional CSS.

How to Create JavaScript Accordion with Arrow

1. First of all, create the HTML structure as follows:

 <div class="accord">
  <div id="accordion">
  <div class="accordionMainTitle">
    <span>FAQ</span>
  </div>
  <div id="panel-wrapper"> 
    <div class="panel active">
     <div class="acc-header">
     <span>What is Codehim?</span>
      <i class="fa fa-chevron-down iconFixer"></i>
    </div>
    <div class="acc-body">
      <p>CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts.</p>
    </div>
  </div>
<div class="panel">
     <div class="acc-header">
     <span>Lorem Ipsum is simply dummy text</span>
      <i class="fa fa-chevron-down iconFixer"></i>
    </div>
    <div class="acc-body">
      <p>But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful.</p>
    </div>
  </div>
<div class="panel">
     <div class="acc-header">
    <span class="acc-header-title">Lorem Ipsum is simply dummy text</span>
      <i class="fa fa-chevron-down iconFixer"></i>
    </div>
    <div class="acc-body">
      <p>But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful.</p>
    </div>
  </div>
   </div>
   
</div>

2. After that, add the following CSS styles to your project:

.panel {
  padding: 20px;
  
}
.accordionMainTitle {
  background:#c1e3ff;
  border: 2px solid #ddd;
  border-radius: 10px 10px 0 0;
  padding: 20px 35px;
  font-family: 'Raleway', sans-serif;
  font-size: 25px;
}

.acc-header {
  cursor: pointer;
  padding: 25px;
  color: #397BB5;
  border-radius: 10px;
  font-weight: bold;
  background-color: #FAFAFA;
  display: block;
  font-family: 'Raleway', sans-serif;
  margin-bottom:0px;
  border-bottom: none !important;
}


.acc-body {
    padding: 20px;
    line-height: 1.9;
    background-color: #FAFAFA;
    border-radius: 0 0 10px 10px;
    font-family: 'Raleway', sans-serif;
}
#accordion .acc-body{
   display : none;
}

#accordion .active .acc-body {
   display : block;
}

#panel-wrapper {
  border: 2px solid #DDD;
  border-top: none;
  border-radius: 0 0 10px 10px;
  display: block;
}
.iconFixer {
  float: right;
}
.acc-header:hover{
  background-color: #397BB5 !important;
  color: white !important;
}

.panel:hover .acc-header {
    background-color: #397BB5 !important;
    color: white !important;
    
}

3. Finally, add the following JavaScript code and done.

function initAccordion(accordionElem){
  
  //when panel is clicked, handlePanelClick is called.           
  function handlePanelClick(event){
      showPanel(event.currentTarget);
  }
//Hide currentPanel and show new panel.  
  
  function showPanel(panel){
    //Hide current one. First time it will be null. 
     var expandedPanel = accordionElem.querySelector(".active");
     if (expandedPanel){
         expandedPanel.classList.remove("active");
     }
     //Show new one
     panel.classList.add("active");
     
  }
  var allPanelElems = accordionElem.querySelectorAll(".panel");
  for (var i = 0, len = allPanelElems.length; i < len; i++){
       allPanelElems[i].addEventListener("click", handlePanelClick);
  }
  //By Default Show first panel
  showPanel(allPanelElems[-1])
}
initAccordion(document.getElementById("accordion"));

var accordionHeader = document.g

That’s all! hopefully, you have successfully integrated this accordion with arrow code snippet into your project. If you have any questions or facing any issues, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X