I’m trying to change a checkbox label in Javascript. The site is WordPress and the form is a plug in, so I have to use a snippet plug in to target the checkboxes.
In brief, when a session is full, the checkbox needs to be greyed out and the label text highlighted in red.
Here’s the code I’m using:
var mon1 = document.getElementById('Mondays_7_1_1');
mon1.disabled = true;
mon1.style.color = "red";
<div>
<input class="" type="checkbox" name="Mondays[]" value="5.00-6.00pm: 4-6 year olds FULL" id="Mondays_7_1_1" /> 5.00-6.00pm: 4-6 year olds FULL<br>
<input class="" type="checkbox" name="Mondays[]" value="6.00-7.00pm: 7-9 year olds" id="Mondays_7_1_2" /> 6.00-7.00pm: 7-9 year olds<br>
<input class="" type="checkbox" name="Mondays[]" value="7.00-8.00pm: 10-14 year olds FULL" id="Mondays_7_1_3" /> 7.00-8.00pm: 10-14 year olds FULL
</div>
<div style="font-weight: normal;color:red;" id="checkbox_Mondays_7_1"></div>
The greying of the box isn’t a problem, but I can’t change the font colour of the label. Does anyone know what the issue might be?
The only way you can do this is to change your markup as the text isn’t part of the checkbox. All you need to do is add labels around the checkboxes and their respective texts, and traverse to the correct label with JS.
Your checkboxes should have
<label>
s anyway, so that when the text is clicked it triggers its checkbox.You can target the label using
parentNode
N.B. If you intend to trigger on the checkbox change, use
change
/onChange
notclick
/onClick
.This is because the Style was only for the INPUT.
I have added a label to the text for the moment.
HTML
JS
JSFiddle
Wrap your text around a
<span>
for example, because it is not a part of the input.Your only workaround then afaik is DOM manipulation. Something in the taste of :
This will work for 1 disable, because the function doesn’t take into account the new modified DOM 😉 http://jsfiddle.net/sskqtL9x/4/
I suggest you prepare your DOM (to match the replace pattern) by wrapping your labels inside
span
s orlabel
s and then run this func after some tweaking is done to it.