You can use document.evaluate
:
Evaluates an XPath expression string and returns a result of the specified type if possible.
It is w3-standardized and whole documented: https://developer.mozilla.org/en-US/docs/Web/API/Document.evaluate
function getElementByXpath(path) { return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; } console.log( getElementByXpath("//html[1]/body[1]/div[1]") );
<div>foo</div>
https://gist.github.com/yckart/6351935
There’s also a great introduction on mozilla developer network: https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript#document.evaluate
Alternative version, using XPathEvaluator
:
Related Posts:
- What does “javascript:void(0)” mean?
- For-each over an array in JavaScript
- Failed to load resource: the server responded with a status of 404 (Not Found)
- require is not defined? Node.js
- What is NODE_ENV and how to use it in Express?
- How to deal with net::ERR_SSL_PROTOCOL_ERROR?
- What is the way of declaring an array in JavaScript?
- How to disable JavaScript in Chrome Developer Tools?
- Encode URL in JavaScript?
- JavaScript null check
- JavaScript “Uncaught TypeError: object is not a function” associativity question
- Angular: mat-form-field must contain a MatFormFieldControl
- JS – ReferenceError: fetch is not defined
- Check if a variable is a string in JavaScript
- Module not found can’t resolve
- How can I get file extensions with JavaScript?
- How to detect Safari, Chrome, IE, Firefox and Opera browser?
- What does on() in JavaScript do?
- AngularJS : Factory and Service?
- What’s the difference between console.dir and console.log?
- Use images like checkboxes
- What is the difference between document.location.href and document.location?
- JavaScript error (Uncaught SyntaxError: Unexpected end of input)
- Preloading images with JavaScript
- Uncaught TypeError: Cannot read property ‘msie’ of undefined
- How to get locator of current web element?
- How to hide a div with jQuery?v
- How do I pass variables and data from PHP to JavaScript?
- How do I set/unset a cookie with jQuery?
- Calling onclick on a radiobutton list using javascript
- How to properly export an ES6 class in Node 4?
- Uncaught Typeerror: cannot read property ‘innerHTML’ of null
- Sorting HTML table with JavaScript
- Relation between CommonJS, AMD and RequireJS?
- How to loop through an array containing objects and access their properties
- Expected linebreaks to be ‘LF’ but found ‘CRLF’ linebreak-style
- how to download file in react js
- TypeError: $(…).DataTable is not a function
- Trying to use fetch and pass in mode: no-cors
- Moment.js transform to date object
- jQuery explode string like PHP
- Call Javascript function from URL/address bar
- Anagrams finder in javascript
- Validate date in dd/mm/yyyy format using JQuery Validate
- CSS Animation onClick
- How to subtract days from a plain Date?
- Stopping a JavaScript function when a certain condition is met
- Showing an image from an array of images – Javascript
- Javascript isnull
- Set value to currency in
- How to add 30 minutes to a JavaScript Date object?
- Highlight a word with jQuery
- Javascript: ‘window’ is not defined
- Javascript – Replace html using innerHTML
- Syntax error: Illegal return statement in JavaScript
- Error: TypeError: $(…).dialog is not a function
- Bootstrap modal – close modal when “call to action” button is clicked
- Fire oninput event with jQuery
- javascript includes() case insensitive
- Reading local text file into a JavaScript array
- SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81
- “NS_ERROR_DOM_BAD_URI: Access to restricted URI denied”
- How can I unset a JavaScript variable?
- D3.js: what is ‘g’ in .append(“g”) D3.js code?
- Correct modification of state arrays in React.js
- Angular 2 Sort table columns
- JavaScript string encryption and decryption?
- How to dequeue a script?
- Combine enqueue js without affecting dependencies
- WP Customizer set a value via javascript (to create presets)
- preserve url parameters from affliliate sites
- How to overwrite JS variable with wp_localize _script?
- wp_enqueue_script adds only the first script
- Add javascript to a woocommerce page template
- How to get value of selected page template in Gutenberg editor?
- Dynamically insert values in a WordPress SelectControl Options field
- Vimeo froogaloop
- What javascript libs & frameworks are integrated in WordPress vanilla?
- Conditional fields in contact form 7 not working
- Enqueueing a script and a style sheet not working
- AngularJS with route and JSON API
- InnerBlocks restricting does not work on nested blocks
- Uppload image from another source wp.media
- Use onfocus event in Contact Form 7
- Get Block Attributes in JSON REST API
- How to open the add media dialogue it in a certain state / tab?
- enqueue script won’t work (enqueue style does work)
- Variable equals another variable and then equals another variable
- Putting custom html/js page into Elementor as it’s own block
- How to get file object from file_attachment?
- Drag-to-Scroll Landingpage
- Easiest way to find JS conflicts
- How do I load urls from an external .js file to my admin head?
- Nothing in dashboard that uses javascript works
- XMLHttpRequest of admin data to public
- How to execute WP shortcode in a javascript string “”
- JS files landing on page but not working, using childtheme of twentyeleven
- PHP or JS for header image rotator?
- Replacing & in JS code on pages
- Use add_action to run a script, but only on the post editor page