You are here: Reference > JavaScript > client-side > HTML DOM > methods > namedItem (form)

namedItem method (form)

Browser support:
Returns an element or a collection of elements from the elements collection of the current form element by name.
The namedItem method is only supported by Internet Explorer and Opera. For a cross-browser solution, use the namedItem method or the [] operator on the elements collection instead. For details, see the page for the elements collection.

Syntax:

object.namedItem (name);
You can find the related objects in the Supported by objects section below.

Parameters:

name
Required. String that specifies a value for the name or id property of the element or elements to retrieve.

Return value:

  • If no match is found, it returns null.
  • If exactly one match is found, it returns the matching element.
  • If more than one match is found, it returns an elements sub-collection filled with the matching elements.

Example HTML code 1:

This example illustrates the use of the namedItem method:
<head>
    <script type="text/javascript">
        function GetByName () {
            var form = document.getElementById ("myForm");

            if (form.namedItem) {   // Internet Explorer, Opera
                var myInputs = form.namedItem ('myInput');
                alert ("The number of controls with name='myInput': " + myInputs.length);
                alert ("The first control with name='myInput':\n" + myInputs[0].value);
            }
            else {
                alert ("Your browser doesn't support the namedItem method.");
            }
        }
    </script>
</head>
<body>
    <form id="myForm">
        <span>Click on this button:</span>
        <button onclick="GetByName ();">Get form controls by name</button>
        <br /><br />
        <input name="myInput" size="40" value="First input field" />
        <br />
        <input name="myInput" size="40" value="Second input field" />
    </form>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

A cross-browser solution for the previous example:
<head>
    <script type="text/javascript">
        function GetByName () {
            var form = document.getElementById ("myForm");

            var myInputs = form['myInput'];
            alert ("The number of controls with name='myInput': " + myInputs.length);
            alert ("The first control with name='myInput':\n" + myInputs[0].value);
        }
    </script>
</head>
<body>
    <form id="myForm">
        <span>Click on this button:</span>
        <button onclick="GetByName ();">Get form controls by name</button>
        <br /><br />
        <input name="myInput" size="40" value="First input field" />
        <br />
        <input name="myInput" size="40" value="Second input field" />
    </form>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content