How can I create an HTML checkbox with a label that is clickable (this means that clicking on the label turns the checkbox on/off)?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Method 1: Wrap Label Tag
Wrap the checkbox within a
labeltag:Method 2: Use the
forAttributeUse the
forattribute (match the checkboxid):NOTE: ID must be unique on the page!
Explanation
Since the other answers don’t mention it, a label can include up to 1 input and omit the
forattribute, and it will be assumed that it is for the input within it.Excerpt from w3.org (with my emphasis):
Using this method has some advantages over
for:No need to assign an
idto every checkbox (great!).No need to use the extra attribute in the
<label>.The input’s clickable area is also the label’s clickable area, so there aren’t two separate places to click that can control the checkbox – only one, no matter how far apart the
<input>and actual label text are, and no matter what kind of CSS you apply to it.Demo with some CSS: