jQuery: Select data attributes that aren't empty?
Just as further reference, and an up-to-date (may'14) (aug'15) (sep'16) (apr'17) (mar'18) (mar'19) (may'20)...
Answer that works with:
Empty strings:
If the
attr
must exist & could have any value (or none at all)
jQuery("[href]");
Missing attributes:
If
attr
could exist & if exist, must have some value
jQuery("[href!='']");
Or both:
If
attr
must exist & has to have some value...
jQuery("[href!=''][href]");
PS: more combinations are possible...
Check this test in jsFiddle for examples:
jQuery v1.11.0 ->
jsFiddle online testjQuery v2.1.0 ->
jsFiddle online testjQuery v2.1.3 ->
jsFiddle online testjQuery v3.0.0-alpha1 ->
jsFiddle online testjQuery v3.1.1 Slim ->
jsFiddle online testjQuery v3.2.1 ->
jsFiddle online testjQuery v3.3.1 ->
jsFiddle online testjQuery v3.4.1 ->
jsFiddle online test Last jQuery version available in jsFiddle at may 28'20jQuery Edge ->
jsFiddle online test jQuery edge version (use with caution)
Or here in SO with this Code Snippet.
* Snippet is running jQuery v2.1.1
try
$(':not([data-go-to=""])')
UPDATE:
For the sake of not leading anyone astray, this answer will work in older versions of jQuery but is not future-proof. Since @gmo and @siva's answers both seem to be working with later versions I defer to (and encourage you to upvote) their answers....and of course hope you have a fantastic day.
$('[data-go-to!=""]:[data-go-to]').each(function() { // Do Your Stuff});