jQuery: Select data attributes that aren't empty?

Shpigford picture Shpigford · May 17, 2012 · Viewed 76.1k times · Source

I'm trying to select all elements that have a data-go-to attribute that is not empty.

I've tried $('[data-go-to!=""]') but oddly enough it seems to be selecting every single element on the page if I do that.

Answer

gmo picture gmo · May 30, 2014

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...