Jquery select all elements that have $jquery.data()

Argiropoulos Stavros picture Argiropoulos Stavros · Feb 29, 2012 · Viewed 73.8k times · Source

Select elements that i have previously set with jquery.data();

i.e. Select all elements with .data('myAttr') already been set.

SOLUTION

A jsfiddle to demostrate is Fiddle

Answer

Nicola Peluchetti picture Nicola Peluchetti · Feb 29, 2012

You could do

$('[data-myAttr!=""]'); 

this selects all elements which have an attribute data-myAttr which is not equal to '' (so it must have been set);

you could also use filter()

$('*').filter(function() {
    return $(this).data('myAttr') !== undefined;
});