0

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.

4 Answers 4

5
$(".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.

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

Comments

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

Comments

1

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");

Comments

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.