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 8844023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:22:55+00:00 2026-06-14T11:22:55+00:00

I’m having some trouble with data-attributes, I can’t get anything to work for some

  • 0

I’m having some trouble with data-attributes, I can’t get anything to work for some reason so I must be doing something wrong:

Set:

$('#element').data('data1', '1'); //Actually in my case the data is been added manually 

Does that make a difference?

Get:

$('#element').data('data1');

Select:

$('#element[data1 = 1]')

None of this works for me, am I making this up or how is it?

  • 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-06-14T11:22:57+00:00Added an answer on June 14, 2026 at 11:22 am

    All of the answers are correct, but I want to state something that nobody else did.
    The jQuery data method acts like a getter for html5 data attributes, but the setter does not alter the data-* attribute.
    So, If you manually added the data (as is stated in your comment), then you can use a css attribute selector to select your element :

    $('#element[data-data1=1]')  
    

    but if you have added (altered) the data via jQuery, then the above solution won’t work.
    Here’s an example of this failure :

    var div = $('<div />').data('key','value');
    alert(div.data('key') == div.attr('data-key'));// it will be false  
    

    So the workaround is to filter the collection by checking the jQuery data value to match the desired one :

    // replace key & value with own strings
    $('selector').filter(function(i, el){
        return $(this).data('key') == 'value';
    });
    

    So, in order to overcome these issues, you need to use the html5 dataset attributes (via jQuery’s attr methos) as getters and setters :

    $('selector').attr('data-' + key, value);
    

    or you can use a custom expression that filters jQuery internal data :

    $.expr[':'].data = function(elem, index, m) {
        // Remove ":data(" and the trailing ")" from the match, as these parts aren't needed:
        m[0] = m[0].replace(/:data\(|\)$/g, '');
        var regex = new RegExp('([\'"]?)((?:\\\\\\1|.)+?)\\1(,|$)', 'g'),
        // Retrieve data key:
        key = regex.exec( m[0] )[2],
        // Retrieve data value to test against:
        val = regex.exec( m[0] );
        if (val) {
            val = val[2];
        }
        // If a value was passed then we test for it, otherwise we test that the value evaluates to true:
        return val ? $(elem).data(key) == val : !!$(elem).data(key);
    };
    

    and use it like :

    $('selector:data(key,value)')
    

    Update

    I know this thread is a few years old, but since it has some activity, it’s worth mentioning that doing this using the querySelector dom API (with no need for jQuery) is quite trivial:

    document.querySelectorAll('[attribute=value]')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.