Are `name`, `id`, and `for` required? Can they be the same? And what is the relationship between them?

Why must there be a “name” and “for” attribute? Can’t the code work without them?

What difference are there between name and id attribute?

9 Likes

the for attribute uses the id to associate input field and label, the id also can be used for styling

19 Likes

Do “id”, “name” and “for” attributes require the same name ?

4 Likes

id and for should have the same name, given this associates the label with the input element. name doesn’t have to be the same, but i quite often do, because you want to have a logic name (one that make sense for the input field)

19 Likes

What happens if we omit the attribute “for” in label tag.Does it makes any difference?

1 Like

the for attribute associates the label with the input element, omitting could cause problems for disability aid tools for example.

4 Likes

Why should the value in name attribute of input should match the value of for attribute of label?
I can understand for(of label) should match with id(of input).
It works just fine when name does not match with for. Why does the exercise insist on both values being the same?

the name attribute doesn’t have too match the for attribute, yet very often these names are the same. Why? Because its the most logic/descriptive name available.

1 Like

in this exercise, why we don’t need to value?
as told previously, the input sent the user input with name=value to site, but here seem its ok to not using value. am i right?

the text area is not self closing, as such the value can be between the opening and closing tag (just like a paragraph, span and so forth), so we don’t need the value attribute

1 Like

thank you, but i ask about password input, not text area (of corse i did did not know the notice you mentioned, thanks for that )

i read text area, so that is what i answered

as for password input, using the value attribute will set the default for the input field (password in this case), we don’t want to set a default password (security risk/nightmare), so the user has to enter their password.

3 Likes

you’r right, thanks for answer. :ok_hand:

how do u give correct usernames and passwords for form submission

Like a login system? You would need to compare the username and password with stored credentials.

id is how your frontend (CSS, JS) identifies the element, and it’s expected to be unique within the page
name corresponds to the form element and identifies what is posted back to the server

7 Likes

hi there, I see the id’s purpose is to link the for attribute in label, since input already has “type” attribute, why can’t we just link the type attribute to label, why create an extra attribute such as id?
also isnt the name attribute quite repetitive, instead of name attribute, shouldn’t there be a " value" attribute that actually shows the password?
thanks for advance!
i wish there are more explanation on the tutorial in this section!

We can easily have multiple input elements of the same type, there is nothing unique about the type attribute

name is the attribute which is used when we submit the form with input fields

value attribute is the initial value.

One of the things I have come to accept, is that these things have generally been thought-through really well. But I can understand the struggle to see through the abstraction and why certain decisions are made.

If you omit “for” in the label attribute, you should then wrap both the label and input attributes in the label in order to link the label and its respective input field.

1 Like