calvin-brand-light-mode
calvin-brand-dark-mode
∕
⌘K
About Calvin Podcast We Can Do This Book Speaking
aAbout Calvin
Podcast
wWe Can Do This Book
sSpeaking
Back

Always use the for attribute on labels

Other
Calvin Correli·Jun 9, 2006· 1 minute

In the same vein as the last post: For a while there, I thought I could take a shortcut by saying:

<label><input type="checkbox" name="foo" value="1" /> The label</label>

Instead of the more cumbersome:

<label for="mycheckbox"><input type="checkbox" name="foo" id="mycheckbox" value="1" /> The label</label>

That way I don’t have to worry about setting the ID for the checkbox, making sure it’s unique, and all that.

It works fine in Firefox, and Safari ignores it, just like it ignores any old label tag. But Internet Explorer, of course, had to not work. So forget about that, and just use the for and id attributes like you always did. Bah.

Previous What's wrong with Jeopardy-style?
Next Use onclick, not onchange for checkboxes

Comments

Write a comment...
00:00 Recording...
calvin-brand-light-mode
calvin-brand-dark-mode
1 Chestnut Hill Plaza #1139
Newark, DE 19713
United States
Copyright © 2026 Calvin Correli
powered by