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 Organizing Forms The Label Element

In order so that the "for"attribute in label tag to work, must each input element have an 'id' ?

Do we need to always set an "id" in the input element in order for the label to work, so that the cursor will appear when we click the label name? I was thinking that the attribute "for" in the label tag has various types like name, email, or passwords. But am I right to think that what we put in the "for" value section of the label tag, is the "id value" we set in the input label? So is the basic structure or syntax to remember in making an input element is not to forget to set an "id", because later on when setting the label tag for that input, label tag will need the "id value" in the "for"attribute section of the label tag?

1 Answer

Steven Parker
Steven Parker
231,268 Points

You're right that the "for" attribute is a reference to the ID of a control element. Control elements that do not already have an ID can be given one when needed for a label to refer to.

Labels can also be associated with a control by containing them. In that case, neither the control ID or the "for" attribute of the label is necessary.

Thank you.