<div class="accordionFaq"></div>
const playersOptions = [
{ source: '...' },
{ source: '...' },
...
];
$('.accordionFaq').html(playersOptions.map((n, i) => `
<div class="accordionFaq-item">
<div class="accordionFaq-head js-accordionFaq-head">
Video #${i}
</div>
<div class="accordionFaq-body">
<div id="player${i}"></div>
</div>
</div>
`).join('')).on('click', '.accordionFaq-head', function(e) {
const index = $(this).closest('.accordionFaq-item').index();
$('.accordionFaq-item', e.delegateTarget).each(function(i) {
const
$this = $(this),
isClicked = i === index,
active = isClicked && !$this.hasClass('active');
$(this)
.toggleClass('active', active)
.find('.accordionFaq-body')
[isClicked ? 'slideToggle' : 'slideUp']();
playersOptions[i].player[active ? 'play' : 'pause']();
});
});
playersOptions.forEach((n, i) => {
n.player = new Clappr.Player({
source: n.source,
parentId: `#player${i}`,
});
});