name gets or sets the name property of an element in the DOM. It only applies to the following elements: <a>, <applet>, <button>, <form>, <frame>, <iframe>, <img>, <input>, <map>, <meta>, <object>, <param>, <select>, and <textarea>.
Note: The name property doesn't exist for other elements; unlike tagName and nodeName, it is not a property of the Node, Element or HTMLElement interfaces.
name can be used in the document.getElementsByName() method, a form and with the form elements collection. When used with a form or elements collection, it may return a single element or a collection.
Syntax
HTMLElement.name = string let elName = HTMLElement.name let fControl = HTMLFormElement.elementName let controlCollection = HTMLFormElement.elements.elementName
Example
<form action="" name="formA"> <input type="text" value="foo"> </form> <script type="text/javascript"> // Get a reference to the first element in the form let formElement = document.forms['formA'].elements[0] // Give it a name formElement.name = 'inputA' // Show the value of the input alert(document.forms['formA'].elements['inputA'].value) </script>
Notes
In Internet Explorer (IE), the name property of DOM objects created using document.createElement() can't be set or modified.
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
name | Chrome No support No | Edge No support No | Firefox ? | IE ? | Opera No support No | Safari ? | WebView Android No support No | Chrome Android No support No | Firefox Android ? | Opera Android No support No | Safari iOS ? | Samsung Internet Android No support No |
Legend
- No support
- No support
- Compatibility unknown
- Compatibility unknown
