Skip to content

Instantly share code, notes, and snippets.

@scy
Last active December 21, 2015 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scy/6232291 to your computer and use it in GitHub Desktop.
Save scy/6232291 to your computer and use it in GitHub Desktop.
A simple :data(key) selector for jQuery
// jQuery UI has a :data selector. I wanted to have it without including jQuery UI.
// To find out how all of this works, read the Sizzle documentation:
// https://github.com/jquery/sizzle/wiki/Sizzle-Documentation
// Thanks to @_Tomalak for inspiring me to check this out.
jQuery.expr.pseudos.data = jQuery.expr.createPseudo(function (key) {
return function (el) {
return !!jQuery.data(el, key);
};
});
@attrib
Copy link

attrib commented Aug 14, 2013

I always used the following

jQuery.extend(jQuery.expr[":"], {
    data : function(el, i, m) {
        return !!jQuery.data(el, m[3]);
    }
});

But yours seems more convenient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment