Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML HTML Forms Fieldsets and Labels

Stacy Fabian
Stacy Fabian
2,246 Points

Why do you need to do that "label for="" and id="" method when you can get the same results another way?

Here's what I originally typed...

<input type="checkbox" name="meal selection" value="sunday_breakfast">Sunday Breakfast<br>

And it resulted in the same field labels as...

<input type="checkbox" name="meal selection" value="sunday_breakfast" id="sunday_breakfast"><label for="sunday_breakfast">Sunday Breakfast</label>

Is there some reason down the line that my original entry method would not work or would result in something looking all wonky or is it just one of those things to have "clean code"? Or is it even because or css styling?

Thanks! Stacy

2 Answers

Well, we know you need to declare an ID for JavaScript/CSS methods and/or styles.

As for the "for" attribute associates the label with a control element as defined in the description of label in the HTML 4.01 spec. When the label element receives focus (e.g. by being clicked on), it passes the focus on to its associated control.

I would suggest checking out the Mozilla Developer Network on the Label Element over on their site, it is a good source for when you get stuck on anything web design or development (in my opinion). I used their site a reference point when I first started web design.

Hope this help,

Stu :)

Stacy Fabian
Stacy Fabian
2,246 Points

Thanks! I'm used to Drupal front end so taking a step back and learning the backend from the true "beginning" is a bit of a mind-bender at times. :)