Skip to content

Instantly share code, notes, and snippets.

@NetanelBasal
Created April 8, 2015 13:03
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 NetanelBasal/f0725e468317e4089301 to your computer and use it in GitHub Desktop.
Save NetanelBasal/f0725e468317e4089301 to your computer and use it in GitHub Desktop.
siblings dom
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div>
<ul>
<li><a href="" class='first'>1</a></li>
</ul>
<p>lorem</p>
<section>sectuib</section>
</div>
</body>
</html>
var el = document.querySelector('section');
function getNextSiblings(elem) {
var sibs = [];
while (elem = elem.nextElementSibling) {
sibs.push(elem);
}
return sibs;
}
function getPrevSiblings(elem) {
var sibs = [];
while (elem = elem.previousElementSibling) {
sibs.push(elem);
}
return sibs;
}
console.log(getPrevSiblings(el));
function getAllSiblings(elem) {
var sibs = [];
elem = elem.parentNode.firstElementChild;
do {
sibs.push(elem);
} while (elem = elem.nextElementSibling)
return sibs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment