Ben casey
Ben casey

Reputation:

Change CSS using "if" in jQuery

Is there a way to change the CSS of a parent div only if the child is visible? And back again when not??? I've been racking my brains and I'm all Googled and jQuery documentationed out.

Upvotes: 0

Views: 292

Answers (4)

redsquare
redsquare

Reputation: 78687

$(".childClass").each(function(){
    var $this = $(this);

    $this.is(":visible") ? $this.parent().css("some_attribute", "some_value")
                         : $this.parent().css("some_attribute", "some_value")
});

Upvotes: 0

Max Schmeling
Max Schmeling

Reputation: 12509

This may not be quite as efficient as Chris Doggett's, but it's shorter... thought I'd throw it out there so you'd have the option:

$("childClass:visible").parent().css("some_attribute", "some_value");
$("childClass:hidden").parent().css("some_attribute", "default_value");

Upvotes: 1

Gregoire
Gregoire

Reputation: 24872

if(($('#childId').css('display')!="none")&&($('#childId').css('visibility')!="hidden"){
  $('#childId').parent().css('property',value);
}

Upvotes: 1

Chris Doggett
Chris Doggett

Reputation: 20777

$(".childClass").each(function(){
    if($(this).is(":visible"))
    {
        $(this).parent().css("some_attribute", "some_value");
    }
    else
    {
        $(this).parent().css("some_attribute", "default_value");
    }
});

You can also use addClass/RemoveClass on the parent, but be careful if you remove a class that you're using as part of the selector to find the child element.

Upvotes: 5

Related Questions