I have created an accordion using the nextUntil()
function. When an accordion item is clicked, I would like to close the previously active item.
HTML:
<div class="accordion">
<h3>Item 1</h3>
<p>...</p>
<p>...</p>
<h3>Item 2</h3>
<p>...</p>
<p>...</p>
<h3>Item 3</h3>
<p>...</p>
<p>...</p>
</div>
jQuery:
$(document).ready(function() {
$('.accordion p').hide();
$('.accordion h3').click(function(e) {
e.preventDefault();
var $this = $(this);
$this.nextUntil('.accordion h3').slideToggle('fast');
});
});
I need the DOM exactly as shown above and cannot have the nested <p>
elements.
Here is a demo of what I have done so far: https://jsfiddle.net/L6q4pako/
I wasn't able to correctly use the siblings()
function to hide the previously active <p>
tags.