0

I need to change the css propery of left div if right div doesn't contain image.

<div class="wrapper">
    <div class="pg-left-bar"></div>
    <div class="pg-right-bar">
        <img src="img.jpg">
    </div>
</div>

http://jsfiddle.net/paycb/1/

If right div has no image then left div width should be change to 100% and margin removed from left div and change the display property of right div to none

I know how to change the css property using jquery only if i am looking for the element in same div

$('.pg-right-bar').has('img').css({ 'background-color': '#fff' });

But i am not sure how i can do this for the above senario.

Any help is appreciated

UPDATE

Jquery Code to achive this

if ( $('.pg-right-bar').has('img').length > 0 )
{
    $('.pg-left-bar').css({ 'background-color': '#ccc' });

}
else
{
     $('.pg-left-bar').css({ 'margin-right': '0px' });
     $('.pg-left-bar').css({ 'width': '100%' });
     $('.pg-right-bar').css({ 'display': 'none' });
}

http://jsfiddle.net/paycb/4/

1 Answer 1

1

Sample
http://jsfiddle.net/paycb/2/

JS

if ( $('.pg-right-bar').has('img').length > 0 )
    $('.pg-left-bar').css({ 'background-color': '#fff' });

jQuery selectors limit the amount of selected elements. Using has() limits the object count in this case to 0 if no img-tag is available. As far as I know this scenario cannot be solved with pure CSS. I gave you an example in JS.

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

2 Comments

I did it based on your reply jsfiddle.net/paycb/4, Please check if this approach is correct.
Looks and work fine for me. I provided a sample and had no doubt you could write the else part :-)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.