Skip to content

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.

Code:

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;}

Usage:

let a = pGS(myElement,true); // Previous Sibling
let b = pGS(yourElement,false); // Next Sibling
let c = pGS(hisElement); // Next Sibling

Return:

A 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.

Params:

pGS(n,p)

n = < Node/Element > * REQUIRED

  • The Node or Element from which to start searching.
  • nodeType not equal to 1 are 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 true to search previous siblings and false to search next siblings.
  • If omitted, pGS will default to searching next siblings.

Clone this wiki locally