Skip to content

Instantly share code, notes, and snippets.

@diogorb
Forked from tit/getelementbyxpath.js
Created October 2, 2020 18:13
Show Gist options
  • Save diogorb/a1720d9fb914340d5bf5d6a8a3eafba7 to your computer and use it in GitHub Desktop.
Save diogorb/a1720d9fb914340d5bf5d6a8a3eafba7 to your computer and use it in GitHub Desktop.
Javascript => getElementByXPath
document.getElementByXPath = function(sValue) { var a = this.evaluate(sValue, this, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); if (a.snapshotLength > 0) { return a.snapshotItem(0); } };
document.getElementsByXPath = function(sValue){ var aResult = new Array();var a = this.evaluate(sValue, this, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);for ( var i = 0 ; i < a.snapshotLength ; i++ ){aResult.push(a.snapshotItem(i));}return aResult;};
document.removeElementsByXPath = function(sValue) { var a = this.evaluate(sValue, this, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); for ( var i = 0 ; i < a.snapshotLength ; i++ ) { a.snapshotItem(i).parentNode.removeChild(a.snapshotItem(i)); } };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment