i'm trying to resolve how to load the content of my tab only when its clicked, not before. And i need to reload the content every time I change to another tab, that's because i need to embed autoplayed videos inside every tab and i need to reload so the video doesn't keep playing on the back. And i need that only one video plays everytime. I've tryed a lot of things and at this moment i doesn't hace anything that i think it will works.
Right now the code for my tabs is the following one:
const tabSystem = {
init() {
document.querySelectorAll('.tabs-menu').forEach(tabMenu => {
Array.from(tabMenu.children).forEach((child, ind) => {
child.addEventListener('click', () => {
tabSystem.toggle(child.dataset.target);
});
if (child.className.includes('is-active')) {
tabSystem.toggle(child.dataset.target);
}
});
});
},
toggle(targetId) {
document.querySelectorAll('.tab-content').forEach(contentElement => {
contentElement.style.display = contentElement.id === targetId ? 'block' : 'none';
document.querySelector(`[data-target="${contentElement.id}"]`).classList[contentElement.id === targetId ? 'add' : 'remove']('is-active');
})
},
};
tabSystem.init();
margin: 0;
font-size: 0;
padding: 0;
}
.tabs-menu {
cursor: pointer;
font-family: 'Inter', sans-serif;
}
.tab-item {
font-size: 15px;
background-color: #A6B7C7;
display: inline-block;
padding: 13px 20px;
color: white;
margin: 0;
}
.is-active {
color: black;
}
.tab-content {
display: none;
padding: 0px;
}
.show-content {
display: block;
background: lightgray;
}
<ul class="tabs-menu">
<li class="tab-item is-active" data-target="first-tab"><a>Español</a></li>
<li class="tab-item" data-target="second-tab" id="Tab02"><a>English</a></li>
<li class="tab-item" data-target="third-tab"><a>Português</a></li>
</ul>
<div class="tab-content" id="first-tab">
<iframe width="560" height="315" src="https://www.youtube.com/embed/7N9XW_mQ7hI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
<div class="tab-content" id="second-tab">
<iframe width="560" height="315" src="https://www.youtube.com/embed/bdvidv6RXJY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
<div class="tab-content" id="third-tab">
<iframe width="560" height="315" src="https://www.youtube.com/embed/rSnaEd-dwTg" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
If anyone have any idea how could solve this i would appreciate it.