Accordion Menu in Vanilla Javascript

I’m back in the mood for more Javascript, more specifically “vanilla javascript” – which is just a phrase for writing Javascript in it’s true form, free from any frameworks or libraries. I guess the only thing we would be dependent on would be the “Javascript Engine” itself and the available “API’s” available for our use. I think this is pretty cool, we can do a lot with just “vanilla javascript” alone.

Now don’t get me wrong, frameworks and libraries are great – they take care of all the nitty gritty cross browser stuff but often come with unnecessary bloat or extra features that aren’t even utilized. Frameworks like ReactJS and VueJS manage DOM manipulation for you so you can offload having to manage a bunch of selectors. There are some key concepts to be learned in writing plain Javascript though, as we shall see – we can achieve the same functionality with much less code.

Below is what it’s going to look like

This post is based off a Youtube video that covers building an accordion menu in plain Javascript. I see similar videos like these a lot but this particular one explains things quite well so all the credit goes to Go Make Things for creating the video. The actual video, you can find here – Learn vanilla JS in about 10 minutes. Yeah, 10 minutes.

The Markup

I’ve intentionally omitted the standard HTML boilerplate stuff – I’m going to go straight into the body section of our HTML page so we can start dissecting some javascript. I’ve also condensed things a bit compared to the video.


<html>
<head>
<title>Javascript Accordion</title>
  
<style>
  .accordion {
    display: none;
  }

  .accordion.active {
    display: block;
  }
  
</style>

</head>


<body>

<p><a class="accordion-toggle" title="" href="#content-1">Show More 1</a></p>
<div class="accordion" id="content-1">
<p>Content 1</p>
</div>

<p><a class="accordion-toggle" title="" href="#content-1">Show More 2</a></p>
<div class="accordion" id="content-2">
<p>Content 2</p>
</div>

<p><a class="accordion-toggle" title="" href="#content-1">Show More 3</a></p>
<div class="accordion" id="content-3">
<p>Content 3</p>
</div>

// We will be adding javascript here... before the body tag

</body>
</html>

First things first, each accordion div section is set to “display: none” using CSS. There are three anchor links – Show More 1, Show More 2, Show More 3. We also have another class called “.accordion.active” to style accordions with the active class. Okay, pretty straightforward.

The Javascript


document.addEventListener('click', function(event) {

     // use negation to select elements with class accordion-toggle
    if (!event.target.classList.contains('accordion-toggle')) {
      return;
    }

    var content = document.querySelector(event.target.hash);

    if (!content) {
      return;
    }

    // Show/hide content
    content.classList.toggle('active');

    // Prevent default link behavior
    event.preventDefault();
  
  }, false);

This is pretty much it – we’ve got a working accordion all written in plain Javascript. I think this is well written Javascript code and there’s a lot we can learn from it. I’ve included some comments above to emphasize on some key techniques for each piece of code.

We can make this better though and I’m glad this feature is included in the video. What if we wanted to hide any open sections and keep the current one open? In my consulting work, I would often write this all in jQuery – but after checking out this javascript, they are extremely similar. So we will use the same code above but modify it a bit.

Implementing a True Accordion Functionality


document.addEventListener('click', function(event) {

  if (!event.target.classList.contains('accordion-toggle')) {
    return;
  }

  var content = document.querySelector(event.target.hash);

  if (!content) {
    return;
  }

  event.preventDefault();

  // Check if our content area is already open
  if (content.classList.contains('active')) {
    content.classList.remove('active');
    return;
  }

  // if not... then run the below...

  // Get all the accordions
  var accordions = document.querySelectorAll('.accordion');

  for (var i = 0; i < accordions.length; i++) {
    accordions[i].classList.remove('active');
  }

  content.classList.add('active');

}, false);

Using vanilla Javascript, we’ve achieved the same accordion functionality we would with either jQuery Core or jQuery’s UI library. There are a lot of fundamental concepts to be learned here though from “if statements“, “the return keyword“, the “classList” API, and the “for loop” just to name a few. Remember to take your time, don’t rush and keep immersing yourself.