I have seen your comments on the answer given by @willpakpoy that you have satisfied with his answer but want to change the text to bold for more inputs at once. So I have modified his answer so that it should work for any number of inputs elements. check the snippet.
I’m trying to turn the label bold when the checkbox is checked.
But my label text doesn’t have any html tag around it so the usual solution :checked + span or :checked + label doesn’t work in this case.
How could I make the label bold in this case? Is it possible with CSS only?
Comments
Comment posted by caniuse.com/css-has
The only way to do this:
Comment posted by Temani Afif
check the duplicate for a CSS only solution
Comment posted by Joe Bloggs
Thanks @TemaniAfif, I think the focus-within solution will work. But how’s the browser compatibility though? I actually think the JS answer may be better in this case.
Comment posted by caniuse.com/css-focus-within
if you don’t care about IE, the support is pretty good:
Comment posted by Joe Bloggs
Hey thanks, I think I’m nearly there. It works, but it only works for the first checkbox out of the 12 in total. Is there anything to change to make it work for all checkboxes in the list?
Comment posted by willpakpoy
I would assume they all share the same class?
Comment posted by Joe Bloggs
I changed the checkbox line from “var checkBox = document.querySelectorAll(“input”)[0];” to “var checkBox = document.querySelectorAll(“ul.list li input”)[0];” would it change anything?
No, it won’t change anything. I updated the JSFiddle.
Comment posted by BOZ
But in the question he asked if it was possible without breaking the structure.
Comment posted by Joe Bloggs
exactly, my setup is other way around. I’ve got input inside label, so this won’t work.
Comment posted by Manas Khandelwal
Ultimately the solution is the same, and the label tag is best used with for attribute. The label text appears next, same as his code. I find this as a clean solution, yes many more solutions are available.
Comment posted by jsfiddle.net/12qLpcje
Ok, see here, this is the correct structure again, see if you can make it working