A lot of frameworks implement their own attributes (ng-
, v-
, bind-
, etc.) Is there a way to select elements that have attributes starting with some string? (without looping through all elements and their properties)
Asked
Active
Viewed 165 times
3

Manuel
- 10,869
- 14
- 55
- 86
-
Do you want to select elements where the attribute name is ng-, or v-, without any regard as to their value? – Jonathon Blok Jul 06 '15 at 23:02
-
Yes, the attribute name starting with some value like ng- or v-, regardless of the value (or what's after ng-/v- in the name) – Manuel Jul 06 '15 at 23:08
-
Yer, that's not possible using CSS selectors alone I'm afraid. See this article on using wildcards, but that is just for an attributes value: http://stackoverflow.com/questions/8714090/queryselector-wildcard-element-match – Jonathon Blok Jul 06 '15 at 23:15
-
Looping through and checking attributes would be the only other way I think. Unless you can work a Regex into querySelectorAll perhaps – Jonathon Blok Jul 06 '15 at 23:16
-
possible duplicate of [Selecting element by data attribute](http://stackoverflow.com/questions/2487747/selecting-element-by-data-attribute) – Travis J Jul 06 '15 at 23:30
-
Not trying to select by data attributes. – Manuel Jul 07 '15 at 00:00
-
I feel your pain. `data-` attributes are fully supported by JavaScript, yet those frameworks choose not to use them. Why oh why? Still, if they would, they would probably do names like `data-ng-whatever` and you'd have the same problem. Oh well. – Mr Lister Jul 07 '15 at 05:27
1 Answers
2
You can use these methods to do this :
Padolsey custom filter (see Padolsey regexp: filter)
Let's say you have
<div class="exampleDiv">
, you can catch the div element with$("div:regex(class, exa.*)")
. (NB:exa.*
is your regex...)Native jQuery selectors
$('[id^=start]')
matches elements with id attribute starting with start$('[id$=end]')
matches elements with id attribute ending with end

Modzful
- 177
- 6