Skip to content

Instantly share code, notes, and snippets.

@OlavHN
Created March 20, 2014 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OlavHN/9663219 to your computer and use it in GitHub Desktop.
Save OlavHN/9663219 to your computer and use it in GitHub Desktop.
<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