-
Notifications
You must be signed in to change notification settings - Fork 24
09. Get Sibling (pGS.js)
Pimp Trizkit edited this page Mar 6, 2018
·
1 revision
| << Previous | Back to Table of Contents | Next >> |
"I think I can find your sister." - PT
This will get the next (or previous) sibling in the container while skipping text nodes. Or rather, it will skip all nodes that are not of nodeType == 1.
const pGS=(n,p)=>{let x=p?n.previousSibling:n.nextSibling;while(x&&x.nodeType!=1)x=p?x.previousSibling:x.nextSibling;if(x&&x.nodeType==1)return x;else return false;}let a = pGS(myElement,true); // Previous Sibling
let b = pGS(yourElement,false); // Next Sibling
let c = pGS(hisElement); // Next SiblingA HTML Element that is a sibling of the specified element. This returned element is not necessarily the adjacent sibling of the specified element. This function will skip nodes without type equal to 1, and return the nearest relevant node. Or false if no relevant node was found.
pGS(n,p)
n = < Node/Element > * REQUIRED
- The Node or Element from which to start searching.
-
nodeTypenot equal to1are skipped, until one is found, and that one is returned.
p = < NextPrevious Boolean > Optional
- A true or false equivalent boolean used to determine which direction to search. Suggested to use
trueto search previous siblings andfalseto search next siblings. - If omitted,
pGSwill default to searching next siblings.
"Programming comfortably with my PJs." - PT