Created
September 20, 2018 20:28
-
-
Save StevenMDixon/f01d0c8c94dda1f1639b8e45113124b6 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
// A simplified way to get the siblings of an element | |
//This function returns an array of sibling elements that does not include the passed element. | |
function getSiblings(htmlElement) { // gets the siblings of the passed element | |
return Array.prototype.filter | |
.call(htmlElement.parentElement.children, child => child.nodeType == 1 && child != htmlElement); | |
} | |
//functionally the same as | |
function getSiblings(htmlElement) { | |
let siblings = []; | |
let current = htmlElement.parentElement.firstElementChild; | |
do { | |
if(current != htmlElement){ | |
siblings.push(current); | |
} | |
} | |
while(current = current.nextElementSibling); | |
return siblings | |
} | |
//and the oversimplified es6 implementation is manditory. | |
function getSiblings(htmlElement){ | |
return [...htmlElement.parentElement.children].filter(i => i != htmlElement); | |
} | |
// the main thing to take away is that we are able to manupulate the dom with return from these functions. | |
getSiblings[0].forEach(i=> i.style.color = 'red'); | |
//can we make it smaller? YES! | |
const a=(e)=>[...e.parentElement.children].filter(i=>e!=i); | |
//of course none of this compatible in IE 8 or less. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment