Created
March 20, 2014 12:59
-
-
Save OlavHN/9663219 to your computer and use it in GitHub Desktop.
This file contains 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
<body></body> | |
<script> | |
var container = document.createElement('div'); | |
for(var i = 0; i<5;i++) { | |
var elem = document.createElement('div') | |
elem.textContent = "I am " + i; | |
container.appendChild(elem); | |
} | |
container.addEventListener('click', function(e) { | |
var clicked = e.target; | |
if (clicked.classList.contains('inactive')) | |
return; | |
if (clicked.classList.contains('active')) { | |
[].slice.call(container.querySelectorAll('div')).forEach(function(elem) { | |
elem.classList.remove('inactive'); | |
elem.classList.remove('active'); | |
}); | |
return; | |
} | |
[].slice.call(container.querySelectorAll('div')).forEach(function(elem) { | |
elem.classList.add(elem === clicked ? 'active' : 'inactive'); | |
}); | |
}); | |
document.body.appendChild(container); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment