Reputation:
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
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
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
Reputation: 7416
It should be $(".foo").show()
, instead of $("#foo").show()
, since foo
is a class, not an id
Upvotes: 0
Reputation: 7428
your mistake was using id instead of class:
if($('.fooz').length)
$('.foo').show();
Upvotes: 2
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);
Upvotes: 0