lundi 30 mars 2015

Change background color if div has specific Child

I am trying to change the background (and other styles) of a div if it has a specific child. I am using .length to check if the specific child exists but even if it doesn't the div is taking the styles from the jquery if statement. My HTML:



<div class="full-text-panel">
<div class="main-wrap">
<h1 class="home-h1">This is a header</h1>
<a class="body-button">This is a button</a>
</div><!--end main-wrap-->
</div><!--end full-text-panel-->


And my jQuery:



var fullText = $('.full-text-panel');
var bodyButton = $('.body-button');

if ( $(".full-text-panel .body-button").length > 0 ) {
fullText.css("background", "red");
} else {
fullText.css("background", "blue");
}


I thought this would work, but both fullText panels are taking the background red style. What am I doing wrong?


Aucun commentaire:

Enregistrer un commentaire