Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alberto-marin/e3c1e7c17b35139dd0ab8094ad851f11 to your computer and use it in GitHub Desktop.
Save alberto-marin/e3c1e7c17b35139dd0ab8094ad851f11 to your computer and use it in GitHub Desktop.
Javascript - Loop through child elements and change its class
/*
**
** Steps
**
*/
var steps = document.getElementById('steps');
steps.firstElementChild.classList.toggle('active');
var children = steps.children;
var i = 0;
function removeActive() {
// Remove all active child
for (var index = 0; index < children.length; index++) {
children[index].classList.remove('active');
}
i += 1;
}
function changeActive() {
removeActive();
if ( i < children.length) {
children[i].classList.add('active');
} else {
if (i == children.length ) {
steps.firstElementChild.classList.add('active');
i = 0;
}
}
}
document.querySelector('button.next').addEventListener("click", changeActive, false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment