Get a list of elements that have attribute names that start with a specific string using XPath

Viewed 54

I'm trying to get an array of elements that have attribute names that start with "temp" (not the attribute value, or I'd use querySelectorAll).

However, it seems that I can't quite get the xpath expression correct. Here is what seems to be the closest I've come up with:

let els = Array.from(document.evaluate(
  `//*/@*[starts-with(name(.), 'temp')]`,
  document,
  null,
  XPathResult.FIRST_ORDERED_NODE_TYPE,
  null
).singleNodeValue);

console.log(els);
<div temp-callback="1" temp-post-callback="4"></div>
<div temp-callback="2"></div>
<div></div>
<div temp-callback="8" temp-post-callback="7"></div>

I would assume that would give me the 3 divs that contains at least 1 attribute with "temp."

What am I missing?

1 Answers
Related