Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

284
Views
In JavaScript, how to use Xpath for String in any variable

This code works. But It's not what I want.

<div>XPath example</div>
<script>
const result = document.evaluate('//div', document, null, 6, null);
console.log(result.snapshotItem(0));
</script>

I want to use Xpath for strings from outside , but the following code is Null.

<script>
var string = "<div>XPath example</div>";
var doc = new DOMParser();
var htmlstring = doc.parseFromString(string, "text/html");
const result = htmlstring.evaluate('//div', document, null, 6, null);
console.log(result.snapshotItem(0));
</script>

How can I use Xpath in Javascript for a string get from the outside?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're passing document as the context node, when you should be passing htmlstring:

const result = htmlstring.evaluate('//div', htmlstring, null, 6, null);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!