Reputation: 219
I want to check whether an element has no hrefs in it (ie it is empty) and if it is, to hide the label associated with it. There will be several elements on the page that need to be checked if they are empty so I will need to write a loop. I need help writing the loop please. Here is my code so far:
if(jQuery('span.tags').is(':empty')) {
jQuery('span.label').hide()
};
Please can someone help me with this code?
This is my HTML
<div class="entry-meta">
<span class="label">Tagged:</span>
<span class="tags">
<a href="#" rel="tag">Career change</a>,
<a href="#" rel="tag">career change e course</a>,
<a href="#" rel="tag">career help</a>
</span>
</div><!-- END .entry-meta -->
Thanks to everyone who answered this post. Thisis the solution that worked.
//Removes word 'Tagged:' if there are no tags
jQuery('span.tags:empty').prev('span.label').hide();
Upvotes: 0
Views: 423
Reputation: 14906
You shouldn't need a loop for that. Based on the assumption that your tags
span will be empty if there are no tags, this will do it:
$('span.tags:empty').prev('span.label').hide();
Upvotes: 0
Reputation: 9031
Very easy:
$('a').each(function(){
if ($(this).attr('href') == ''){
$(this).hide();
}
});
Coded up using you example - http://jsfiddle.net/ajthomascouk/DfM79/
Upvotes: 0
Reputation: 14747
Based from your HTML, you should be able to pull this off even without throwing out an explicit loop.
// something like
$('span.tags').not(':has(a[href])')
.prev('span.label')
.hide()
;
Upvotes: 1