0

I'm working on a tagging feature and the wrapping element for the tag (span class="tag") is showing without content. I need to hide the empty one and show the tags with content. The problem is my logic is affecting all the tags, not just the empty one. How can I target just the empty tag?

 $(".tag:empty").hide();
        $('.tag_btn').live('click', function() {
             if (!$('.tag:empty')){
                 $('.tag').show();
             }
        });
1
  • How about hiding the .tag_btnthat's right next to the empty ones? $(".tag:empty").sibling('.tag_btn').hide(); Commented Jul 8, 2011 at 19:05

2 Answers 2

1

EDIT: Ok so when you click .tag_btn, you want to display all the non-empty .tags ? Try this:

$('.tag_btn').live('click', function() 
{
    $('.tag:not(:empty)').show();
});
Sign up to request clarification or add additional context in comments.

3 Comments

+1 Your answer is better than mine. Good catch seeing that $('.tag:empty') is a selector and not a boolean...
tag_btn is the selector for the button which generates the tag.
Thanks. I wasn't aware you could apply the seudo-class :not in jQuery. I'm having a new issue of the tag still being hidden after '.tag_btn' is clicked. It's not until the second tag is created that the two non-empty tags are displayed. I figured the live event would catch that. Oh well..
0

I'm not sure what could be going on, but would this alternate way work?

$('.tag').each(function(){
    var tag = $(this);
    if ($.trim(tag.text() != ""))
    {
        tag.show();
    }
});

Comments

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.