JQuery selectors by attribute of user data that is not empty

I want to create a simple custom hint plugin for jQuery, which for each item with data-custom-tooltipset . So something like:

 <a href= . . . " data-custom-tooltip="This is my tooltip Text">Hhahaha</a> 

OR

 <button data-custom-tooltip="This is my tooltip for the button Tex">Haha Button :) </button > 

Thus, the tooltip display function is launched only if data-custom-tooltip does NOT matter.

Close enough to that: jQuery selectors for user data attributes using HTML5

-one
javascript jquery
source share
3 answers

You can use : not () selector and remove empty

 $('[data-custom-tooltip]:not([data-custom-tooltip=""])') 

or

 $('[data-custom-tooltip]').not('[data-custom-tooltip=""]') 

or based on @VisioN in the comments with Not Equal Selector

 var xxx = $('[data-custom-tooltip][data-custom-tooltip!=""]'); 
+2
source share

use

 $("[data-custom-tooltip]:not([data-custom-tooltip='']").click(function(){alert("clicked");}); 

fiddle

+1
source share

Try

. filter ()

 var tooltip_el = $('[data-custom-tooltip]').filter(function () { return $.trim($(this).data('custom-tooltip')) != ''; }); 
0
source share

All Articles