user3109580
user3109580

Reputation:

Hide class if other class exists

I have two div classes

<div class="foo"> </div>
<div class="fooz"> </div>

I want "foo" to hide if "fooz" doesn't exists. Actually "foo" is a conditional class and needed when "fooz" exists otherwise hidden.

i found this code browsing stackflow questions

if($('.fooz').length)
$('#foo').show();

But it not working!

Any help how to do this?

Thank you

Upvotes: 1

Views: 3116

Answers (6)

Jay Harris
Jay Harris

Reputation: 4271

// if there is at-least 1 element with .fooz class
//   Show .foo
// else
//   hide .foo
var tog = $('.fooz').length ? 'show' : 'hide';

$('.foo')[tog]();

Upvotes: 0

Ringo
Ringo

Reputation: 3965

In your cass, foo is not an id but it's a class. So you should use:

if($('.fooz').length)
    $('.foo').show();

Upvotes: 0

scrblnrd3
scrblnrd3

Reputation: 7416

It should be $(".foo").show(), instead of $("#foo").show(), since foo is a class, not an id

Upvotes: 0

Felix
Felix

Reputation: 38102

It should be:

$('.foo').show();

since it's a class not an ID

Upvotes: 1

falinsky
falinsky

Reputation: 7428

your mistake was using id instead of class:

if($('.fooz').length)
$('.foo').show();

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388316

foo is a class use class selector, also I think you can use toggle() here to set the display

$('.foo').toggle($('.fooz').length > 0);

Demo: With fooz, without

Upvotes: 0

Related Questions