JSFiddle - React, Tailwind, and code Playground
by gmo lop
HTML
<div class="test_1">
<h4>Test 1: jQuery('a[href]')</h4>
<a href="test">href: test</a>
<a href="#">href: #</a>
<a href="">href: empty</a>
<a href>href: empty</a>
<a>href: not present</a>
</div>
<div class="test_2">
<h4>Test 2: jQuery('a[href!=""]')</h4>
<a href="test">href: test</a>
<a href="#">href: #</a>
<a href="">href: empty</a>
<a href>href: empty</a>
<a>href: not present</a>
</div>
<div class="test_3">
<h4>Test 3: jQuery('a[href!=""][href]')</h4>
<a href="test">href: test</a>
<a href="#">href: #</a>
<a href="">href: empty</a>
<a href>href: empty</a>
<a>href: not present</a>
</div>
CSS
/****************************************
* Title : jQuery: Select data attributes that aren't empty?
* URL : http://stackoverflow.com/questions/10641258/jquery-select-data-attributes-that-arent-empty
* jQuery : v2.1.3
****************************************/
div,a {
display: block;
color: #333;
margin: 5px;
padding: 5px;
border: 1px solid #333;
}
h4 {
margin: 0;
}
a {
width: 200px;
background: #ccc;
border-radius: 2px;
text-decoration: none;
}
a.match {
background-color: #16BB2A;
position: relative;
}
a.match:after {
content: 'Match!';
position: absolute;
left: 105%;
}
JavaScript
// If the attr must exist & could have any value (or none at all)
jQuery('div.test_1 > a[href]').addClass('match');
// If attr could exist & if exist, must have some value
jQuery('div.test_2 > a[href!=""]').addClass('match');
// If attr must exist & has to have some value...
jQuery('div.test_3 > a[href!=""][href]').addClass('match');