I need to retrieve all child-elements where the attribute-name (note: not the attribute-value) starts with a given string.
So if I have:
<a data-prefix-age="22">22</a> <a data-prefix-weight="82">82</a> <a href="#">meh</a>
My query would return a collection of two elements, which would be the first two with the data-prefix--prefix
Any ideas on how to write up this query?
I was going for something like:
But of course that is not valid
Hopefully one of you has a more keen eye on how to resolve this.
(See accepted code example below)
There is apparently no direct way to query on partial attribute names. What you should do instead (this is just one possible solution) is
- select the smallest possible collection of elements you can
- iterate over them
- and then for each element iterate over the attributes of the element
- When you find a hit, add it to a collection
- then leave the loop and move on to the next element to be checked.
You should end up with an array containing the elements you need.
Hope it helps :)
Perhaps this will do the trick -
selectedElements will now hold the first two matching elements.