Create a gist now

Instantly share code, notes, and snippets.

Embed
document.addEventListener('DOMContentLoaded', function(){
var tabs = {
tab1: document.getElementById('tab-link-1'),
tab2: document.getElementById('tab-link-2'),
tab3: document.getElementById('tab-link-3'),
tab4: document.getElementById('tab-link-4')
}
var content = {
one: document.getElementById("tab-content-1"),
two: document.getElementById("tab-content-2"),
three: document.getElementById("tab-content-3"),
four: document.getElementById("tab-content-4")
}
for (tab in tabs) {
if(tabs[tab]) {
tabs[tab].addEventListener('click', function(event) {
event.preventDefault();
var $this = this;
clearSelected();
$this.classList.add("selected");
clearActive();
if ($this.id === "tab-link-1") {
content.one.classList.add("active");
} else if ($this.id === "tab-link-2") {
content.two.classList.add("active");
} else if ($this.id === "tab-link-3") {
content.three.classList.add("active");
} else {
content.four.classList.add("active");
}
});
}
}
var has_edd_error = document.getElementsByClassName('edd-alert');
if (has_edd_error.length > 0) {
clearSelected();
tabs.tab4.classList.add("selected");
clearActive();
content.four.classList.add("active");
}
function clearSelected() {
for (tab in tabs) {
tabs[tab].classList.remove("selected");
}
}
function clearActive() {
for (element in content) {
content[element].classList.remove("active");
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment