Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to associate label with checkbox without using "for=id"?

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.

like image 949
D.Tate Avatar asked Dec 16 '11 17:12

D.Tate


People also ask

Do checkboxes need an ID?

The ID on your checkbox is necessary because the ID field is what the label's for attribute references, and you need a relationship between the label and the checkbox as I previously mentioned. Show activity on this post. Certain fields are inherently unique; names, id's and label names.

How do you put a label over a checkbox?

All you have to do is use a "br" tag after each ending "label" tag. In other words, by using the "br" or break tag, you're telling the checkbox to "break" down to the next line that is available. Hope that helps!

Do you need label for checkbox?

Without label tags, users can't click the label to tick the checkbox. Instead, they have to click the checkbox itself. This causes users to struggle because the checkbox is a small target to hit especially for motor-impaired users.


1 Answers

Yes, place the input inside the label.

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

See implicit label association in the HTML specifications.

like image 104
Madara's Ghost Avatar answered Sep 29 '22 06:09

Madara's Ghost