I need to select elements based on values stored in an element's .data()
object. At a minimum, I'd like to select top-level data properties using selectors, perhaps like this:
$('a').data("category","music");
$('a:data(category=music)');
Or perhaps the selector would be in regular attribute selector format:
$('a[category=music]');
Or in attribute format, but with a specifier to indicate it is in .data()
:
$('a[:category=music]');
I've found James Padolsey's implementation to look simple, yet good. The selector formats above mirror methods shown on that page. There is also this Sizzle patch.
For some reason, I recall reading a while back that jQuery 1.4 would include support for selectors on values in the jquery .data()
object. However, now that I'm looking for it, I can't find it. Maybe it was just a feature request that I saw. Is there support for this and I'm just not seeing it?
Ideally, I'd like to support sub-properties in data() using dot notation. Like this:
$('a').data("user",{name: {first:"Tom",last:"Smith"},username: "tomsmith"});
$('a[:user.name.first=Tom]');
I also would like to support multiple data selectors, where only elements with ALL specified data selectors are found. The regular jquery multiple selector does an OR operation. For instance, $('a.big, a.small')
selects a
tags with either class big
or small
). I'm looking for an AND, perhaps like this:
$('a').data("artist",{id: 3281, name: "Madonna"});
$('a').data("category","music");
$('a[:category=music && :artist.name=Madonna]');
Lastly, it would be great if comparison operators and regex features were available on data selectors. So $(a[:artist.id>5000])
would be possible. I realize I could probably do much of this using filter()
, but it would be nice to have a simple selector format.
What solutions are available to do this? Is Jame's Padolsey's the best solution at this time? My concern is primarily in regards to performance, but also in the extra features like sub-property dot-notation and multiple data selectors. Are there other implementations that support these things or are better in some way?
At the moment I'm selecting like this:
Which seems to work just fine, but it would be nice if jQuery was able to select by that attribute without the 'data-' prefix.
I haven't tested this with data added to elements via jQuery dynamically, so that could be the downfall of this method.
You can also use a simple filtering function without any plugins. This is not exactly what you want but the result is the same:
I've created a new
data
selector that should enable you to do nested querying and AND conditions. Usage:The pattern is:
"operator" and "check" are optional. So, if you only have
:data(a.b.c)
it will simply check for the truthiness ofa.b.c
.You can see the available operators in the code below. Amongst them is
~=
which allows regex testing:I've tested it with a few variations and it seems to work quite well. I'll probably add this as a Github repo soon (with a full test suite), so keep a look out!
The code:
It works. See Attribute Equals Selector [name=”value”].
I want to warn you that
$('a[data-attribute=true]')
doesn't work, as per Ashley's reply, if you attached data to a DOM element via the data() function.It works as you'd expect if you added an actual data-attr in your HTML, but jQuery stores the data in memory, so the results you'd get from
$('a[data-attribute=true]')
would not be correct.You'll need to use the data plugin http://code.google.com/p/jquerypluginsblog/, use Dmitri's
filter
solution, or do a $.each over all the elements and check .data() iterativelyThere's a
:data()
filter plugin that does just this :)Some examples based on your question:
The performance isn't going to be extremely great compared to what's possible, selecting from
$._cache
and grabbing the corresponding elements is by far the fastest, but a lot more round-about and not very "jQuery-ey" in terms of how you get to stuff (you usually come in from the element side). Of th top of my head, I'm not sure this is fastest anyway since the process of going from unique Id to element is convoluted in itself, in terms of performance.The comparison selector you mentioned will be best to do in a
.filter()
, there's no built-in support for this in the plugin, though you could add it in without a lot of trouble.