Syed Mahfuzul Mazid
Syed Mahfuzul Mazid

Reputation: 91

Jquery check if a div(several divs with same class) contains a div with class on

i have three divs with class UserDetail. if a div doesnot contain div.info then have to hide that div.UserDetail

Update from comment:

I have tried this:

var childInfo=$('div.UserDetail').find("div.info").size(); 
if(childInfo==0) $('div.UserDetail').hide(); 

This is not working as all the divs contains same class name and other two divs have the child div class=info

Upvotes: 1

Views: 1524

Answers (4)

ostapische
ostapische

Reputation: 1592

jsFiddle

HTML

<div class="userDetails">1</div>
<div class="userDetails">2<div class="info"></div></div>
<div class="userDetails">3</div>

JS

$.each( $( "div.userDetails" ), function( i, div ) {
    if ( $( div ).find( "div.info" ).length == 0 ) {
        $( div ).hide();
    }
} );

Upvotes: 0

MrPeeps
MrPeeps

Reputation: 575

$(".UserDetail").each(function (index) {
        if ( $(this).find('div.info').length < 0) {
            $(this).hide();
        }
    });

Upvotes: -1

nraina
nraina

Reputation: 354

I think this might work

$.each($('div.UserDetail'), function(i, value){
     if($(value).find('div.info').length == 0)  
        {
          $(value).hide();            
        }
});

Upvotes: 1

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93561

Use :has with :not selector:

JSFiddle: http://jsfiddle.net/TrueBlueAussie/5pbap/

$('.UserDetail:not(:has(.info))').hide()

or in full (if you care about the divs): http://jsfiddle.net/TrueBlueAussie/5pbap/1/

$('div.UserDetail:not(:has(div.info))').hide()

Upvotes: 1

Related Questions