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 Choosing Options Radio Buttons

Andrew Hoskins
Andrew Hoskins
4,888 Points

the purposes behind using: "value", "id", "name", and "for" ?

Hello, I am having trouble fully understanding the purposes behind using: "value", "id", "name", and "for"
I would assume that id is used in correlation with javascript to help identify items, but I am not sure - and as for the other listed items, I am at a loss. Could someone please try to explain it to me as simply as possible? Thanks!

4 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Andrew,

I'll jump straight in.

  • The value attribute is for the input fields data value, when the form gets posted through to a backend language such as PHP; the value can be extracted to perform additional tasks if needed

  • The id attribute can be used a few different ways, most importantly it's a reference for targeting the element using a CSS selector, it can also be targeted using JavaScript and can also be targeted using the for attribute on a label

  • The name attribute is the most important on a form element, without it a - backend language wouldn't have any idea that it ever existed thus no value for it would ever been seen

  • Finally the for attribute is important to label element's, as I said earlier this allows you to target the id attribute of the form field which is great for accessibility and it provides a way for the user to visually see where they are if they can only navigate using a keyboard as well as when they're using a screen reader.

Andrew Hoskins
Andrew Hoskins
4,888 Points

Thanks very much - both answers help greatly.

idan ben yair
idan ben yair
10,288 Points

Hi Andrew try reading here a little bit:

http://www.w3.org/TR/html401/interact/forms.html#initial-value

w3.org has a lot of info on it! it's great I use it a lot when I cant understand something.

Let me know if that helped :)