0

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();
6
  • 3
    Can you show us some html you would be using this code on. Commented Jun 16, 2011 at 9:45
  • possible duplicate of How to perform a " For " loop in jquery? Commented Jun 16, 2011 at 9:46
  • for the loop, check the .each method in jQuery Commented Jun 16, 2011 at 9:48
  • Can you give an example of an empty tag too? It's not clear from your question whether the empty tags have no href attribute at all, or simply an empty href. Commented Jun 16, 2011 at 10:21
  • Hello Town, did you remove your answer? Your solution works perfectly, thank you! By the way, the empty tags did not have hrefs at all. Commented Jun 16, 2011 at 10:24

4 Answers 4

1

You can use the each function to loop through DOM elements, and hide each one.

$('span.tags:empty').each(function(i, value) {
    $('span.label').hide();
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Josh, I tried that but it removes the span.label even if they are not empty.
@madame: Because $('span.label') selects every span.label element. See @richardneililagan's answer.
That won't work because you're selecting every span.label in the DOM inside your .each() function. You have to find some way to limit the span.labels you select to be related to the corresponding span.tags.
1

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()
              ;

2 Comments

Thanks Richard but this does not work. I still see the Tagged: label even if the element is empty.
Did you remember to put your jQuery code inside a $(document).ready() handler?
0

Very easy:

$('a').each(function(){
    if ($(this).attr('href') == ''){
        $(this).hide();
    }
});

Coded up using you example - http://jsfiddle.net/ajthomascouk/DfM79/

2 Comments

... except that that hides the <a> elements, not the corresponding span.label.
my bad... let me relook at it.
0

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();

Demo on jsfiddle

1 Comment

Simple, neat, and fast.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.