I have a selector that pulls in all anchor tags that start with the #
symbol. I am trying to add a :not
selector of some form for elements that have a data
attribute. I know I could do the following...
$('a[href^="#"]:not([data-known])')
But the problem is that I don't know what the data
attribute will be called. I have resorted to doing the following.
$('a[href^="#"]').click(function(event){
if($.isEmptyObject($(this).data())){
//custom code here
}
});
Is there a way to avoid the if
statement and add the :not
to the original selector? Is there a better way of doing this period?
Here is a FIDDLE I have been playing in.
isEmptyObject
under the hood, but with a lot of extra code that's not needed in this case. \$\endgroup\$hasData
simply won't work at all.hasData
only checks for data that has been set via.data()
. It doesn't automatically pull in the HTML5data-*
attributes. \$\endgroup\$