What is name Attribute in HTML?
The 'name' attribute specifies a them to an element that is used during form submission and as a reference for other purposes.
- It is used to identify the input value if it is sent to a server via a traditional GET or POST of a form.
- Unlike id, the name attribute may not be unique across the entire page.
- The name attribute can be used as a reference by JavaScript, HTML, CSS, and in submitted form data.
Syntax
Example
- In the above example, the name attribute in <form> is used to reference the form data after form submission.
- The name attribute in <select> identifies the dropdown list (with 3 values - S, M, L).
Supported elements
The elements that support the name attribute are as follows: <button>,<fieldset>,<form>, <iframe>, <input>, <keygen>,<map> ,<meta>,<object>,<output>, <param> , <select>,<textarea>
Type of Value
- The value acts as an identifier. That is, it is a name used to identify the element.
- The ‘name’ attribute does not have any default value.
Value
It is generally a string value.
Supported doctypes
The ‘name’ attribute for elements like button, textarea, select, form, img, a input, object, map, and param is supportext areaTML 4.01 strict, HTML 4.01 transitional, and HTML 4.01 frameset.
4.01 frameset for the frame, iframe.
More Examples
Examples of name attributes with their supporting elements:
button
The name attribute in <button> is used to reference the form data after form submission or reference the element in JavaScript. It contains a single value name that describes the name of the <button> element.
Example
Output
textarea
The HTML <textarea> name Attribute is used to reference the form data after the form submission or reference the element in JavaScript. The value assigned to the name attribute is used to identify a specific textarea element.
Example
Output
select
The name Attribute in <select> specifies a name for the drop-down list. It is used to reference the form data after form submission or to reference the element in JavaScript.
Example
Output
form
The HTML <form> name Attribute is used to reference the form data after form submission or reference the element in JavaScript. It is a single value name that specifies the name of the <form> element.
Example
Output
fieldset
The name Attribute in the element <fieldset> is used to refer to all input elements of the fieldset. It is used to reference the form data after submitting the form or to reference the element in JavaScript.
Example
Output
frame
The name attribute specifies the name of a <frame> element. The name attribute is used to reference the element in JavaScript or to act as a target for a link. The frame tag is not supported in HTML5.
Example
iframe
The name attribute in the <iframe> element is used to identify an <Iframe> Element. The name attribute is also used as a reference to the elements in Javascript.
Output
img
The name attribute in the` element is used to identify an image. The name attribute is obsolete in HTML5 (id can be used instead of it
Example
Output
a
The name attribute is used in Anchor Tag to specify the name of an anchor and “jump” to a specific point on a web page. It is mainly used in large pages or subdivisions. The name attribute for the <a> tag is not supported in HTML5 (id can be used instead).
Example
Output
input
The <input> name attribute is used to specify a name for an <input> element and references the form data after submitting the form or to reference the element in JavaScript.
Example
Output
object
The name attribute in <object> is used to specify the name of the embedded file. This attribute is also used as a reference for an object element in Javascript.
Example
Output
map
The name attribute in <map> is used to specify the name of the image map and is associated with the <img> usemap attribute. It creates a relationship between the image and the map.
Example
Output
param
The value set to the name Attribute of <param> is used to specify a name for the <param> element. The name attribute is used along with the value attribute to define a parameter for plug-ins associated with the <object> element.
Example
Output
meta
The HTML <meta> name Attribute is used to specify the name for the metadata. This attribute is used to define the name for the information/value of the content attribute. Either the http-equiv attribute or the name attribute should be set.
Example
What is the Purpose of the HTML name Attribute?
The name attribute identifies a specific element to contribute to the form data sent to a server. Thus, in the case of submitting the value of the control to the server-side processing, the name attribute is a must.
However, it can be neglected if no control value is submitted. For example, the name attribute can be ignored, if there is a single submit button. However, if there are multiple submit buttons the name attribute is used to recognize which button was used on the server side.
The name attribute is allowed and recognized in some elements, disallowed, and ignored in others, and when allowed, its meaning is defined in the definition of the element.
Supported Browsers
The browsers and their first version that supported the name attribute are as follows:
Browser | Version |
---|---|
Google Chrome | 1.0 |
Internet Explorer | 1.0 |
Firefox | 1.0 |
Apple Safari | 1.0 |
Opera | 1.0 |
Learn more
To know more about Attributes in HTML head over to Attributes in HTML.
Conclusion
- The name attribute acts as an identifier for an element.
- This attribute helps in referencing elements in JavaScript.
- It plays a major role in submitting the value of the control to the server side.
- The name attribute is supported by some elements like <button>, <form>, <input>, <select>, <textarea>, etc.