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" />
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.
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.
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>.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With