What I’m trying to do is when the checkbox is checked, change the background color of the div, and when it’s unchecked, remove the background color. How can I do this using jquery?
<div class="checkbox-container">
<input type="checkbox" class ="checkbox-border" id="personal-info-checkbox">
<label for="personal-info-checkbox"> Mark as reviewed and acknowledged
</label>
</div>
using parent selector and .removeclass I do not know how to select my div and turn the color off and on using jquery.
5
Answers
Add a
change
event listener to theinput
that sets its closest parentdiv
‘s background color based on whether it is checked:Try this I hope this will help you
Here’s an example of how you can do this
There are a number of ways to do this:
The below example has explanatory comments in the code:
References:
:has()
.:checked
.:has()
.var()
.document.querySelector()
.Element.classList
API.is()
.on()
.toggleClass()
.You don’t need jQuery for this
You can do this only with css.
:has
pseudo class is supported in chromium, safari.For firefox, need to enable flag.
know more at mdn ::has pseudo class