Skip to content

Instantly share code, notes, and snippets.

@rainyjune
Created February 13, 2013 03:16
Show Gist options
  • Save rainyjune/4941980 to your computer and use it in GitHub Desktop.
Save rainyjune/4941980 to your computer and use it in GitHub Desktop.
DOM manipulation
function firstElementChild(e) {
var e = e || document.documentElement;
return e.firstElementChild || e.children[0] || null;
}
function lastElementChild(e) {
var e = e || document.documentElement;
return e.lastElementChild || e.children[e.children.length-1] || null;
}
function nextElementSibling(e) {
if(e.nextElementSibling) return e.nextElementSibling;
for(e = e.nextSibling; e && e.nodeType !== 1; e = e.nextSibling);
return e;
}
function previousElementSibling(e) {
if(e.previousElementSibling) return e.previousElementSibling;
for(e = e.previousSibling; e && e.nodeType !== 1; e = e.previousSibling);
return e;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment