Skip to content

Instantly share code, notes, and snippets.

@dvlden
Created October 14, 2017 13:42
Show Gist options
  • Save dvlden/56e8e0d0cb939cbe4c2db3cd7a80fbd1 to your computer and use it in GitHub Desktop.
Save dvlden/56e8e0d0cb939cbe4c2db3cd7a80fbd1 to your computer and use it in GitHub Desktop.
Some JS Testing...
const getSiblings = element => {
let siblings = []
let sibling = element.parentNode.firstChild
while (sibling = sibling.nextSibling) {
if (sibling.nodeType === 1 && sibling !== element) {
siblings.push(sibling)
}
}
return siblings
}
const removeActiveSiblingClass = (currentElement, activeClass = 'active') => {
let activeElement = currentElement.parentElement.querySelector('.active')
if (activeElement) {
activeElement.classList.remove(activeClass)
}
}
document.querySelector('#dummy').addEventListener('click', (e) => {
e.preventDefault()
if (e.target.tagName === 'LI') {
removeActiveSiblingClass(e.target, 'active')
e.target.classList.add('active')
}
}, false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment