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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would probably pass the ID of x to doit(),… May 16, 2026 at 4:47 pm
  • Editorial Team
    Editorial Team added an answer Actually, there is a Thread.exit() method in the Sun implementation… May 16, 2026 at 4:47 pm
  • Editorial Team
    Editorial Team added an answer This is the same as configuring the proxy for any… May 16, 2026 at 4:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a SELECT element in which I need to auto-select the appropriate option
I'm working on a program that needs to be able to load object-properties from
In my app i want the users to select a base colour for elements
I have n number of select elements in an html page that are used
I need to determine which element comes first in my markup, such as an
I have the Zend Form with some elements. And I need to placing some
I'm inside a function and I need to return a jQuery object with two
I have loaded an array in javascript with a select amount of input element
Consider this simple xml element example: <parent foo=1 bar=2 foobar=3> <child/> </parent> In the
Ok here goes: I have a SELECT drop down option with US state names

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.