Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the HTML name attribute?

Tags:

What is the purpose of the name attribute on input, span, or div tags? Should an id tag be used instead?

Example from Mozilla's documentation:

<label for="User">Click me</label> <input type="text" id="User" name="Name" /> 
like image 459
northben Avatar asked Sep 26 '14 14:09

northben


People also ask

What is the purpose of value attribute in HTML?

The value attribute specifies the value of an <input> element. The value attribute is used differently for different input types: For "button", "reset", and "submit" - it defines the text on the button. For "text", "password", and "hidden" - it defines the initial (default) value of the input field.

What is name and value attribute in HTML?

The purpose of the HTML name attribute is to specify a name for an element. Value of the name attribute works as an identifier of the element. Supported elements. The HTML name attribute supports button, textarea, select, form, frame, iframe, img, a, input, object, map, param and meta elements.

Which tag has name attribute in HTML?

The name attribute on an element assigns a name to that element. This name is used during form submission and as a reference for other purposes. Elements that accept this attribute include <input>, <select>, <textarea>, <button>, <iframe>, <map>, <meta>, <output>, <fieldset>, <object>, and <param>.

What is the purpose of for attribute?

The for attribute is an allowed attribute for <label> and <output> . When used on a <label> element it indicates the form element that this label describes. When used on an <output> element it allows for an explicit relationship between the elements that represent values which are used in the output.


1 Answers

The name attribute identifies the input value if it is sent to a server via a traditional GET or POST of a form.

Specific to the example if you had:

<form action="http://localhost" method="POST">     <label for="User">Click me</label>     <input type="text" id="User" name="Name" value="foo" />     <input type="submit" value="submit" /> </form> 

and you submit the form, the server localhost will receive a content body like:

Name=foo 

which as another post mentions, is usually parsed by a server side language like PHP into something easier to work with.

The id attribute identifies the input to the DOM. If you specify an input with no name but an id and try to submit it via a GET or POST it will not be parsed correctly by the server.

like image 190
user3357118 Avatar answered Nov 03 '22 09:11

user3357118