Wednesday, 28 April 2021

JavaScript evaluate XPATH within an element?

I am trying to get an element using document.evaluate() but also want to search only within a specific element. So for example:

const element = document.evaluate('.//p', ...); //I want this to return the Hello, World p element
<html>
  <body>
    <div id="someId">
      <p>Hello, World!<p>
    </div>
  </body>
</html>

Is there any way I could pass in the div with id someId into the evaluate to only search within that scope?

I know I could write the whole XPATH like .//div[@id="someId"]/p (Wouldnt work for my case) or do string concatenation but would like to find a cleaner way of doing it like passing the DOM element (or some object it contains) somewhere.



from JavaScript evaluate XPATH within an element?

No comments:

Post a Comment