this selects all elements which have an attribute data-myAttr which is not equal to '' (so it must have been set); (only for elements that have their data attribute set in HTML not via jQuery)
you could also use filter() (which works for data attributes set from jQuery)
Using $('[data-myAttr!=""]') doesn't work for all cases. Because elements that do not have a data-myAttr set, will have their data-myAttr equal to undefined and $('[data-myAttr!=""]') will select those as well, which is incorrect.
To get all elements matching .data('myAttr') you can use
var matches = $(':data(myAttr)');
This should work for both elements with data- attributes and elements with data stored using $.data() because
As of jQuery 1.4.3 HTML 5 data- attributes will be automatically pulled in to jQuery's data object.
But this doesn't work very well. Check this jsfiddle and you will see that the second time the selector is called it should match 2 elements and is only matching one. This is due to "a bug" in the jquery-ui library.
As you can see they are using $.data(elem, match) instead $(elem).data(match) which causes the cache not to be updated in case you are requesting elements with data- attributes. If the element has been tested for data() storage this works well but if not it will not be included in the resulting matches.
This might not be a bug at all if what you want is to match only elements with data information set by you but if not you are left with two options.
Don't use jquery-ui and extend your own pseudo-selector $(:mydata(myAttr))