2

I want to hide an element if the span element inside a div has no text

below is sample HTML

<div class="showcase-caption" style="display: block;">
 <span> If this span has no text then i need to change the display property of outer div to none</span>
</div>

JsFiddle link

I tried to do this using css but it didn't work as expected. so i think jquery can work better and handle older browser also.

In this case i need to change the inline property which is generated automatically by other script for showing image caption for image gallery.

Update: Thank you guys for your replies. I am choose 'satpal' solution as it is short & gave me exact result.

0

5 Answers 5

4

Just use :empty. You don't need if block.

$('.showcase-caption span:empty').parent().hide()

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

Updated Demo

Use jQuery is() and :empty pseudo selector.

if( $('.showcase-caption span').is(':empty') ) {
    $('.showcase-caption').hide();
} 

2 Comments

It still show the green bar due to padding i want to hide it completey
@sudharsan, in my demo i had written hide() code on span click. So used this
1

Try to use .is() along with :empty selector to accomplish your task,

if($('.showcase-caption span').is(':empty')) { 

DEMO

Full code:

var cache = $('.showcase-caption');
if($('span', cache).is(':empty')) {
  cache.hide();
} 

DEMO


Or you can simply write by using the :has() selector,

$('.showcase-caption:has(span:empty)').hide();

DEMO

Comments

0
if($(".showcase-caption span").html().trim().length <= 0 ){
            $(".showcase-caption").hide();
}

Comments

0

Try this one.

$(function){
    if($('.showcase-caption span').is(':empty')){
        $('.showcase-caption').css("display" , "none");
    }

    }

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.