I have 2 .school
, several .class-room
and there are some .my-child
inside.
If .my-child
is located in the first .class-room
of a school, I want to style that school with yellow background. But it doesn't show as expected.
jsfiddle
$('.school').each(function() {
$(this).find('.my-child').each(function(i, el) {
$(this).addClass('child' + (i + 1));
});
if ($('.school .class-room:first-child').children().hasClass('.my-child')) {
$('.school').css('background', 'yellow');
}
})
.my-child.child1 {
background: red;
}
.my-child.child2 {
background: green;
}
.my-child.child3 {
background: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="school">
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
</div>
<br><br>
<div class="school">
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
</div>
You cannot use the dot in hasClass:
change:
.hasClass('.my-child')
to:
.hasClass('my-child')
I would also use $(this) in your conditional to only select the correct school to change the background color of:
if ( $(this).children('.class-room:first-child').children().hasClass('my-child') )
https://jsfiddle.net/5wxaoj1m/
Edit: Updated to address concerns from comments and I added a jsfiddle link.
I would suggest moving the code for selecting the school outside of your each
loop. Then, use the :has()
selector to find the school.
$('.school:has(.class-room:first-child>.my-child)').addClass('has-my-child');
.my-child.child1 {
background: red;
}
.my-child.child2 {
background: green;
}
.my-child.child3 {
background: blue;
}
.has-my-child { background-color: yellow; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="school">
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
</div>
<br><br>
<div class="school">
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
</div>
Note that this might be slower than using plain old JavaScript:
const school = Array.from(document.querySelectorAll('.school')).filter(school => school.querySelectorAll('.class-room:first-child > .my-child').length === 1)[0];
if (school) {
school.classList.add('has-my-child');
}
.my-child.child1 {
background: red;
}
.my-child.child2 {
background: green;
}
.my-child.child3 {
background: blue;
}
.has-my-child {
background-color: yellow;
}
<div class="school">
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
</div>
<br><br>
<div class="school">
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
<div class="class-room">
<div class="my-child">Child</div>
</div>
<div class="class-room">
<div class="other">Child</div>
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With