Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 821377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:37:48+00:00 2026-05-15T02:37:48+00:00

I need to select elements based on values stored in an element’s .data() object.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:37:48+00:00Added an answer on May 15, 2026 at 2:37 am

    I’ve created a new data selector that should enable you to do nested querying and AND conditions. Usage:

    $('a:data(category==music,artist.name==Madonna)');
    

    The pattern is:

    :data( {namespace} [{operator} {check}]  )
    

    “operator” and “check” are optional. So, if you only have :data(a.b.c) it will simply check for the truthiness of a.b.c.

    You can see the available operators in the code below. Amongst them is ~= which allows regex testing:

    $('a:data(category~=^mus..$,artist.name~=^M.+a$)');
    

    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:

    (function(){
    
        var matcher = /\s*(?:((?:(?:\\\.|[^.,])+\.?)+)\s*([!~><=]=|[><])\s*("|')?((?:\\\3|.)*?)\3|(.+?))\s*(?:,|$)/g;
    
        function resolve(element, data) {
    
            data = data.match(/(?:\\\.|[^.])+(?=\.|$)/g);
    
            var cur = jQuery.data(element)[data.shift()];
    
            while (cur && data[0]) {
                cur = cur[data.shift()];
            }
    
            return cur || undefined;
    
        }
    
        jQuery.expr[':'].data = function(el, i, match) {
    
            matcher.lastIndex = 0;
    
            var expr = match[3],
                m,
                check, val,
                allMatch = null,
                foundMatch = false;
    
            while (m = matcher.exec(expr)) {
    
                check = m[4];
                val = resolve(el, m[1] || m[5]);
    
                switch (m[2]) {
                    case '==': foundMatch = val == check; break;
                    case '!=': foundMatch = val != check; break;
                    case '<=': foundMatch = val <= check; break;
                    case '>=': foundMatch = val >= check; break;
                    case '~=': foundMatch = RegExp(check).test(val); break;
                    case '>': foundMatch = val > check; break;
                    case '<': foundMatch = val < check; break;
                    default: if (m[5]) foundMatch = !!val;
                }
    
                allMatch = allMatch === null ? foundMatch : allMatch && foundMatch;
    
            }
    
            return allMatch;
    
        };
    
    }());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I select an element based on its css? I need to select
I need to select a specific grange of jquery elements based on there index
how can i express in xpath a) need select elements which contains specified keyword.
I need to select elements without child node (including text since in <p> text
I need to select elements of a certain class except children of $(this). The
I need to select elements depending on first letter of text value of a
OK, I have two HTML select elements and a button. I need to disable
I have some elements with (.) periods in them and I need to select
I have these elements, and I need to select the li inside the ul
I need to clear the selection from a <select> element. I've already read such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.