Today we have a “BeefUp” jQuery plugin which we will use to create a nested accordion. Its a highly configurable and multi-purpose plugin that helps you to create the accordion easily.
You don’t need to write all the code from the start. Just implement this plugin and you are done. All you have to design it according to your project requirements.
The “BeefUp” accordion comes with basic design without having too much style. That’s means, you can easily apply your styles.
Furthermore, the nested accordion plugin comes with a lot of awesome options that you can use to change its way of work. It has an animation like sliding, fading, and scrolling.
Moreover, the plugin is perfect to use if you are planning to build an FAQ page or want to use it for general-purpose accordion navigation.
Additionally, you can also trigger your custom callback functions on accordion initialization, open and close.
Because it supports the nested option so you can easily add the multiple groups which can be expanded/collapse their content when selected.
You are allowed to add the text, images, iframe and video content inside the accordion widget or inside the nested accordion.
How to Create jQuery Nested Accordion
To create the FAQs page or a nested accordion widget on your website, You need to load the jQuery library into your webpage.
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>After that, you need to load the plugins files. The plugin comes with two files which include CSS and Javascript file.
<!-- BeefUp CSS -->
<link rel="stylesheet" href="css/jquery.beefup.css">
<!-- BeefUp JS -->
<script src="js/jquery.beefup.min.js"></script>Now let’s create the basic HTML structure which is easy to understand. Each list of the accordion element consists of an article element with a class beefup.
Next, we will place the heading of the accordion and body content. Have a look at the single accordion markup.
<article class="beefup example">
<h4 class="beefup__head">
Item 1
</h4>
<div class="beefup__body">
<div class="mockup">Your content goes here</div>
</div>
</article>
Let’s see you want to build a nested according to then our markup will look like:
<article class="beefup example">
<h4 class="beefup__head">
Item 1
</h4>
<div class="beefup__body">
<article class="beefup example">
<h4 class="beefup__head">
Nested Item
</h4>
<div class="beefup__body">
<div class="mockup mockup-lg">Content for nested items goes here.</div>
</div>
</article>
</div>
</article>The Javascript
Finally, we need to initialize the plugin by calling the jQuery document ready function to active the BeefUp plugin.
$(document).ready(function(){
$('.example').beefup();
});The plugin comes with a lot of options. Let’s says you want to keep open the accordion item then you simply need to define the function like:
// Open single
$('.example-opensingle').beefup({
openSingle: true,
stayOpen: 'last'
});Similar, if you want to change the default animation into fade then your function will look like:
// Fade animation
$('.example-fade').beefup({
animation: 'fade',
openSpeed: 400,
closeSpeed: 400
});Advanced Configuration Options for jQuery Nested Accordion
Let’s check out the following advanced configuration options to create or customize the nested according to the plugin according to your needs.
| Option | Default | Type | Description |
|---|---|---|---|
| accessibility | true | Boolean | Enable/disable accessibility features like tab control. Example:
$('.example').beefup({
accessibility : true,
}); |
| trigger | ‘.beefup__head’ | String | The selector of the trigger element.Example:
$('.example').beefup({
trigger : '.beefup__head',
}); |
| content | ‘.beefup__body’ | String | The class name of collapsible content.Example:
$('.example').beefup({
content : '.beefup__body',
}); |
| openClass | ‘is-open’ | String | The name of the class that determines that accordion item is open or not.Example:
$('.example').beefup({
openClass : 'is-open',
}); |
| animation | ‘slide’ | String | Animation that toggles contents. Possible options are “slide” and “fade”.Example:
$('.example').beefup({
animation : 'slide',
}); |
| openSpeed | 200 | Number | It defines the open speed in milliseconds. Example:
$('.example').beefup({
openSpeed : 200,
}); |
| closeSpeed | 200 | Number | The close speed in milliseconds.Example:
$('.example').beefup({
closeSpeed : 200,
}); |
| scroll | false | Boolean | Decide whether to scroll the window on the content toggle. Example:
$('.example').beefup({
scroll : false,
}); |
| scrollSpeed | 400 | Number | The scrolling speed in milliseconds.Example:
$('.example').beefup({
scrollSpeed : 400,
}); |
| scrollOffset | 0 | Number | It is useful to set additional offset. Example:
$('.example').beefup({
scrollOffset : 0,
}); |
| openSingle | false | Boolean | Close other opened elements when opening a new one.Example:
$('.example').beefup({
openSingle : false,
}); |
| stayOpen | null | Null | Remain items to be open. You can pass selector, item index value in this option. Example:
$('.example').beefup({
stayOpen : null,
}); |
| selfBlock | false | Boolean | Block close event on click. Example:
$('.example').beefup({
selfBlock : false,
}); |
| selfClose | false | Boolean | Decide whether to close when clicking outside the accordion. Example:
$('.example').beefup({
selfClose : false,
}); |
| hash | true | Boolean | Open accordion with id on hash change.Example:
$('.example').beefup({
hash : true,
}); |
| onOpen | null | Function | Callback function to execute on accordion open. Example:
$('.example').beefup({
onOpen : null,
}); |
| onClose | null | Function | Custom function to run on a close event. Example:
$('.example').beefup({
onClose : null,
}); |
Changelog
Oct 5, 2019
- Add accessibility option
- Update button styles
- Prefix private methods
- Use jest instead of QUnit
09/28/2019
- Update sass-lint rules and browsers.
- Update the demo layout.
- Optimize the code.

I love this accordion except for the fact that W3.org recommends that the up and down arrows and home and end keys allow you to navigate between the accordion buttons. I modified your code to add a “beefup__trigger” class to the button created by the javascript and then added the below (much borrowed from the above website). It would be really nice if the arrow keys and home and end keys worked natively on your code.
I would have used the example on the above website, but it does not handle nested accordions well.
$(document).on('keydown', 'button', function(event) { var target = event.target; if (target.classList.contains('beefup__trigger')) { var key = event.which.toString(); var ctrlModifier = (event.ctrlKey && key.match(/33|34/)); var triggers = $('#'+target.id).parents('form:eq(0),body').find('.beefup__trigger').filter(':visible'); if (key.match(/38|40/) || ctrlModifier) { var index = triggers.index(target); var direction = (key.match(/34|40/)) ? 1 : -1; var length = triggers.length; var newIndex = (index + length + direction) % length; triggers[newIndex].focus(); event.preventDefault(); } else if (key.match(/35|36/)) { // 35 = End, 36 = Home keyboard operations switch (key) { // Go to first accordion case '36': triggers[0].focus(); break; // Go to last accordion case '35': triggers[triggers.length - 1].focus(); break; } event.preventDefault(); } } });Hi Janice!
Thanks for your feedback. Keep visiting us for more free code and scripts.