Quick Tip: Easier Way To Use Labels on Input Fields
There's no doubting that adding a Label to things like checkboxes and radio buttons is a godsend for usability.
With a Label
Here's a checkbox with a Label on the label. Click the text to the right of the check box and it toggles the field.
Nice and easy.
Without A Label
Here's a checkbox without a Label:
I'd like to receive the newsletter.Harder to use, isn't it. There's no excuse not to use one!
Using Labels
As I've always understood it, the proper way of doing it is like this:
<input id="signup" value="yes" type="checkbox" name="signup"> <label for="signup">I'd like to receive the newsletter.</label>
Notice the Input element needs an ID and we need to pair this ID with the Label using the Label's "for" attribute. This is what has always put me off using Labels - they're a pain to implement - particularly when hand-coding lots of Inputs.
However, there's an easier way. You can do this:
<label><input value="yes" type="checkbox" name="signup"> I'd like to receive the newsletter.</label>
No need for any IDs!
This comes in handy when programmatically creating groups of checkboxes (or radio buttons) as you only need to wrap them in Label tags, rather than assigning each a unique ID, like so:
<p> <label><input type="radio" value="Red" name="FaveColor" /> Red</label> <br /> <label><input type="radio" value="Green" name="FaveColor" /> Green</label> <br /> <label><input type="radio" value="Blue" name="FaveColor" /> Green</label> </p>
Simples.
Ah! Nice! Makes sense too and like you (probably) I wonder why this wasn't obvious earlier. Thanks for the revelation!
Reply
I thought it made sense at first, but, the more I think about it, the more I wonder.
I Tweeted it earlier and got shot down for suggesting the use of something with a complete lack of semantic meaning.
While I'm all for semantically-correct markup I can't see the harm in this one.
Reply
Show the rest of this thread
I remember picking up this tip on Dan Cederholm's (SimpleBits.com) first book. His books are a must for any web developer.
Reply
Very cool, great tip!
Reply
I'm just glad ExtJS remembers to do all this stuff for me, because I'd never remember. :P
Reply
I'm with the semantic guys on this one. You're making the input a child element of a label. Conceptually this is not correct.
I agree that in practice there is little harm to it though. The only downside of this method is that you become less flexible in repositioning elements through CSS since you have explicitly created this parent-child relationship. Any repositioning of your label will now also reposition your input. I'm sure it won't matter much in this case.
I would never preach semantics for the sake of semantics alone, but there definitely are real world advantages to semantic markup, the most important ones being flexibility, maintainability and how well search engines and screen readers can interpret the markup. So unless there is a very strong reason for non-semantic markup, I'd go for semantic markup. I really don't see what the big win is of not having to supply "id" atributes.
Just my 2 cents...
Reply
Well, I think the win is it's easier if you hand code a lot. If you're using a framework to do this for you, it should be semantically correct out of the box without you needing to think about it.
So, this is handy for folks like me who make a lot of web based tools just for my use or those in my immediate internal group.
<philosophyzimigating>
To your point, though, one could argue semantics are misconceived in the case of labels and controls. The label is the meaning, the control is meaningless outside of the context of the label. So while convention is focused on a semantic that supports a legacy hierarchy where controls are often thought of first by the developer and the label secondarily, to the user, the label is the meaning and it might be worth while to revise semantics in that direction. What meaning has a control apart from its label? One label can be applied to any number of controls and still have meaning.... but a control with no label is meaningless.
</philosophyzimigating>
Reply
Show the rest of this thread
My preference is to use a modern Web development language like Lotus XPages. I just drag a checkbox or radio button control onto the page, specify the text for the Label and it automatically generates the code to produce the behavior described in the example. No HTML required. Now that's RADD!
Reply
Did you just call Xpages a web development language?? LOL
Reply
Ha ha ha. No HTML required too? Fancy, a HTML-less website, all powerd by lotus!
Reply