Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery data selector
    primarykey
    data
    text
    <p>I need to select elements based on values stored in an element's <code>.data()</code> object. At a minimum, I'd like to select top-level data properties using selectors, perhaps like this:</p> <pre><code>$('a').data("category","music"); $('a:data(category=music)'); </code></pre> <p>Or perhaps the selector would be in regular attribute selector format:</p> <pre><code>$('a[category=music]'); </code></pre> <p>Or in attribute format, but with a specifier to indicate it is in <code>.data()</code>:</p> <pre><code>$('a[:category=music]'); </code></pre> <p>I've found <a href="http://james.padolsey.com/javascript/a-better-data-selector-for-jquery/" rel="noreferrer">James Padolsey's implementation</a> to look simple, yet good. The selector formats above mirror methods shown on that page. There is also this <a href="http://jsbin.com/otoqo/edit" rel="noreferrer">Sizzle patch</a>.</p> <p>For some reason, I recall reading a while back that jQuery 1.4 would include support for selectors on values in the jquery <code>.data()</code> 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?</p> <p>Ideally, I'd like to support sub-properties in data() using dot notation. Like this:</p> <pre><code>$('a').data("user",{name: {first:"Tom",last:"Smith"},username: "tomsmith"}); $('a[:user.name.first=Tom]'); </code></pre> <p>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, <code>$('a.big, a.small')</code> selects <code>a</code> tags with either class <code>big</code> or <code>small</code>). I'm looking for an AND, perhaps like this:</p> <pre><code>$('a').data("artist",{id: 3281, name: "Madonna"}); $('a').data("category","music"); $('a[:category=music &amp;&amp; :artist.name=Madonna]'); </code></pre> <p>Lastly, it would be great if comparison operators and regex features were available on data selectors. So <code>$(a[:artist.id&gt;5000])</code> would be possible. I realize I could probably do much of this using <code>filter()</code>, but it would be nice to have a simple selector format.</p> <p>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? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload