What is the purpose of the html name attribute?

What is the purpose of the attribute name

in tags input

, span

or div

? Should a tag be used id

?

An example from the Mozilla Documentation :

<label for="User">Click me</label>
<input type="text" id="User" name="Name" />

      

+3


source to share


5 answers


The attribute name

identifies the input value

if it is submitted to the server via traditional GET

or POST

forms.

Specifically, for 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 the content body, for example:



Name=foo

      

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

The attribute id

identifies input to DOM

. If you specify an input without name

, but id

also try to send it through GET

or POST

, it will not be correctly parsed by the server.

+5


source


The name should be used for form fields such as <input>

, it should not be used in <span>

or <div>

. The name has been deprecated on anchor tags and you should use an identifier instead.

Check the mozilla link :



name gets or sets the name property of the DOM object; this applies only to the following elements: <a>

, <applet>

, <button>

, <form>

, <frame>

, <iframe>

, <img>

, <input>

, <map>

, <meta>

, <object>

, <param>

, <select>

and<textarea>

+3


source


In an element, input

an attribute name

defines the name of the control. Only those controls that have a name can be "successful", that is, they can contribute to the form data set submitted to the server. Thus, it is necessary in order to make the value of the control submitted for processing on the server side. The attribute id

has nothing to do with this; it has its own uses, such as helping to associate a label with a control, as in the example.

Hence, the attribute is name

not needed unless you need to enter a reference value. For example, if you have one submit button, for example <input type=submit value=Send>

, you don't need that; but if you have multiple submit buttons and need to recognize the server side which one was used then you need to. If the form data is not submitted to server-side processing, but is processed only during client-side processing, the attribute is name

not used because you can access the values ​​in other ways.

In span

and div

elements of an attribute name

is not valid for all versions of HTML. If used, it will simply be ignored, except that it is stored (like any invalid attributes) in a property attributes

in the DOM and can be accessed by client-side scripts.

Given the general question, as in the question title, the attribute is name

allowed and recognized in some elements, forbidden and ignored in others, and when allowed, its value is defined in the element definition. There is not much in common between these definitions.

Some confusion has been caused by statements such as "the attribute is name

deprecated / deprecated". Such statements are true for this attribute on some elements in some versions of HTML, but, for example, on input

elements, it remains official and necessary.

+2


source


If you define a link (a-Element) and define a name for it, you can use it as an anchor. See: http://www.w3schools.com/tags/att_a_name.asp

or more broadly: https://developer.mozilla.org/en-US/docs/Web/API/Element.name

+1


source


The name attribute is used when you are using an additional language such as PHP to read and write data to a database or other files. To refer to the text box above, you must provide its name, which is "Name", and write the value in the text box "Name" to the database.

Hope this helps clarify the situation.

0


source







All Articles