You are here: Reference > JavaScript > client-side > HTML DOM > properties > name (input, select, textarea, ...)

name property (input, select, textarea, ...)

Browser support:
Sets or retrieves the name of a form control that affects the contents of the message submitted to the server.
  • The name property is useful for submitting data to a server. The contents of the submitted message represent the state of the named form controls in a form tag.
    For details, see the page for the form element.
  • The name property has additional meaning for radio buttons. You can define groups with the name property of the radio buttons. Radio buttons with the same name belong to the same group. Radio buttons with different names belong to different groups. Within a group, only one radio button can be checked.
  • The name property has effect on Autocomplete windows, too. You can enable the Autocomplete window for an input:password or an input:text element in a form with the autocomplete property. The contents of the Autocomplete window depend on the value of the name property. If you don't want to change the value of the name property because of the Autocomplete window, you can use the vcard_name property in Internet Explorer.
    See the page for the autocomplete or the vcard_name property for further details on Autocomplete windows.
The name property can be used as a reference on the client side. You can get an array of elements that have the same name with the getElementsByName method.

Syntax:

object.name;
You can find the related objects in the Supported by objects section below.
This property is read/write.
HTML page for this property: name

Possible values:

String that sets or retrieves the name of the form control.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the name attribute for form elements:
<form action="#URL#" method="post">
    User Name: <input type="text" name="userName" />
    <br />
    Password: <input type="password" name="password" />
    <br />
    <input type="submit" value="Sign in" />
</form>
Did you find this example helpful? yes no

Example HTML code 2:

This example illustrates the use of the getElementsByName method:
<head>
    <script type="text/javascript">
        function OnRegister () {
            var sexRadios = document.getElementsByName ("sex");
            for (var i = 0; i < sexRadios.length; i++) {
                if (sexRadios[i].checked) {
                    break;
                }
            }
            if (i == sexRadios.length) {
                alert ("Please select your sex!");
                return false;   // cancels the submit
            }
            return true;
        }
    </script>
</head>
<body>
    <form method="post" action="#URL#" onsubmit="return OnRegister ();">
        <input type="radio" name="sex" value="female" />female
        <br />
        <input type="radio" name="sex" id="radio_male" value="male" />male
        <br /><br />
        <input type="submit" value="Register" />
    </form>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content