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

JavaScript Interactive Web Pages with JavaScript Selecting Elements and Adding Events with JavaScript Perform: Selecting Elements

Joe Williams
Joe Williams
4,014 Points

Unsure of what the HTML label is doing if I'm already using an Input ID

I'm working on the Interactive Web Design course, which is tying in a lot of the principles we've been working on -- finally meshing together HTML, CSS and Javascript. However, none of the videos I've watched up to this point have included inputs and labels. I may need to do some higher HTML courses.

Regardless, in the example below, I'm not sure completely what's happening:

  <p>
        <label for="new-task">Add Item</label><input id="new-task" type="text"><button>Add</button>
  </p>

If the input ID "new-task" is already identifying the text field, why am I labeling the Add Item title, which is just a title? What do they have to do with each other?

2 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

So with labels, you use the ID of the element it's labeling in the for attribute. In this case, since your input has an id of "new-task", then the label is "for" that ID, "new-task".

With other input elements like checkboxes and radio buttons, doing this allows the label to be clickable as if it were the input, allowing you to use CSS to "hide" the checkbox/radio button and style the label instead.

Nicholas Woods
Nicholas Woods
6,260 Points

I know this is an old post, but if there are others who are unsure on this, there is a Treehouse course on HTML Forms which explains everything: http://teamtreehouse.com/library/html-forms