Created
May 24, 2018 08:48
-
-
Save alberto-marin/e3c1e7c17b35139dd0ab8094ad851f11 to your computer and use it in GitHub Desktop.
Javascript - Loop through child elements and change its class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
** | |
** 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