Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  140] [ 2]  / answers: 1 / hits: 32138  / 13 Years ago, fri, december 16, 2011, 12:00:00

I know that it is good sometimes to associate a label with a checkbox:



<input id=something type=checkbox value=somevalue />
<label for=something>this is label text</label>


..but do I have to use an id to associate it?

The main reason I even care is because I like being able to click a label to toggle the checkbox value, but don't like the idea of using an id for something so simple.



I guess I could use jQuery toggle the previous element (checkbox) of a clicked label, but maybe there is something simpler I'm missing. https://stackoverflow.com/a/2720771/923817 looked like a solution, but the user said it doesn't work in IE.


More From » jquery

 Answers
84

Yes, place the input inside the label.



<label><input type=checkbox name=chkbx1> Label here</label>


See implicit label association in the HTML specifications.


[#88521] Thursday, December 15, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marques

Total Points: 366
Total Questions: 108
Total Answers: 111

Location: Burundi
Member since Wed, Nov 25, 2020
4 Years ago
;