querySelector, wildcard element match?

Erik Andersson picture Erik Andersson · Jan 3, 2012 · Viewed 118.2k times · Source

Is there a way to do a wildcard element name match using querySelector or querySelectorAll? I see support for wildcards in attribute queries but not for the elements themselves.

The XML document I'm trying to parse is basically a flat list of properties and I need to find elements that have certain strings in their names.

I realize the XML document is probably in need of a restructuring if I need this but that's just not going to happen.

Any solution except going back to using the apparently deprecated XPath (IE9 dropped it) is acceptable.

Answer

JaredMcAteer picture JaredMcAteer · Jan 3, 2012

[id^='someId'] will match all ids starting with someId.

[id$='someId'] will match all ids ending with someId.

[id*='someId'] will match all ids containing someId.

If you're looking for the name attribute just substitute id with name.

If you're talking about the tag name of the element I don't believe there is a way using querySelector