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,618 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 :)