What is the use of the name attribute in <input> tag

Name: can anyone please tell me why it is used here? it doesnt seem to impact the output if it isnt used

This will be output for the submited data! Like for <input type="number" name="phone number blablabla">

The name attribute specifies a name for the element.

This name attribute can be used to reference the element in a JavaScript.

For form elements it is also used as a reference when the data is submitted,

For iframe element it can be used to target a form submission.

2 Likes

More info. Iā€™d suggest bookmarking MDN and using it.


1 Like

As allensolly said correctly, the name is a unique identifier for an element in a form. Basically, if you would have for instance multiple text elements with the same name, they would not work. The form will not receive all inputs from the user. But if you need to do a radio button (multiple choice, the name should be the same):

<input type="radio" name="gender" value="male">
<input type="radio" name="gender" value="female">

Then gender will return only 1 value, whichever you check.

Therefore, it does impact the output.

Here is an example of a simple contact from from a website.

<form action="/contact/" method="post">
<input type="text" name="your-name" value="" size="40" />
<br />
<input type="text" name="your-email" value="" size="40" />
<br />
<textarea name="your-message" cols="40" rows="6"></textarea>
<br />
<input type="submit" value="SUBMIT" />
</form>

As you can see, in order to receive two different inputs for name and e-mail, I have to name them differently. Hope that helps.