namedItem method (form)
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:
You can find the related objects in the Supported by objects section below.
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?
|
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?
|
Supported by objects:
HTML elements:
Related pages:
External links:
User Contributed Comments