Visit Mozilla.org

DOM:element.name

From MDC

« Gecko DOM Reference

Contents

[edit] Summary

name gets or sets the name attribute of an DOM object, it only applies to the following elements: anchor, applet, form, frame, iframe, image, input, map, meta, object, option, param, select and textarea.

Name can be used in the 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.

[edit] Syntax

HTMLElement.name = string;
var elName = HTMLElement.name;

var fControl = HTMLFormElement.elementName;
var controlCollection = HTMLFormElement.elements.elementName;

[edit] 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
  var 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>

[edit] Notes

In Internet Explorer (IE), the name property of DOM objects created using createElement can't be set or modified.

[edit] Specification

W3C DOM 2 HTML Specification: