If the label
element contains ‘false’ is it possible to change the class
of the parent div
? And only from the one?
<div class="myclass">
<input type="checkbox" name="water-list" value="school-1" case="registered" id="school-1" class="CheckBox">
<label for="true">bla</label>
</div>
<div class="myclass">
<input type="checkbox" name="water-list" value="school-2" case="registered" id="school-2" class="CheckBox">
<label for="true">bla</label>
</div>
<div class="myclass">
<input type="checkbox" name="water-list" value="school-3" case="registered" id="school-3" class="CheckBox">
<label for="false">bla</label>
</div>
<div class="myclass">
<input type="checkbox" name="water-list" value="school-4" case="registered" id="school-4" class="CheckBox">
<label for="true">bla</label>
</div>
2
Answers
Yes, it is possible to achieve this using JavaScript. You can add an event listener to each checkbox, and when the checkbox is clicked, check the value of the associated label. If the value is "false," you can then change the class of the parent div.
First you should use a data attribute for your
false
value. (for
is used specifically to associate a label with a corresponding form control element (likeinput
.) I’ve useddata-check
here.Then it depends whether one label will only ever be checked
false
or whether you might have n number of labels checkedfalse
.One label checked false
More than one label checked false
Additional documentation
closest
querySelector
querySelectorAll
dataset