The Document: getElementsByName of method and examples of use

  • 2020-03-26 21:43:31
  • OfStack

Definition and usage

The getElementsByName() method returns a collection of objects with the specified name.
Syntax document. GetElementsByName (name)

This method is similar to the getElementById() method, but it queries for the name attribute of the element, not the id attribute.

Also, because the name attribute in a document may not be unique (for example, radio buttons in an HTML form usually have the same name attribute), all getElementsByName() methods return an array of elements, not an element.

The instance
<script type="text/javascript"> 
function getElements() { 
var x=document.getElementsByName("myInput"); 
<input name="myInput" type="text" size="20" /><br /> 
<input name="myInput" type="text" size="20" /><br /> 
<input name="myInput" type="text" size="20" /><br /><br /> 
<input type="button" onclick="getElements()"value="How many elements named 'myInput'?" /> 

Getelementsbyid, which can only be used by the document object, returns the first element of the array

"Gets the collection of objects based on the value of the NAME tag attribute." Set loose more than an array, set in each item type can be different, set just put certain elements together as a class to use, compared with the array is much more strict, the type of each item is unified. The document. The getElementsByName, document. The getElementsByTagName, document. FormName. Elements it is the result of this kind of method is set. When used for batch update of data transfer.

Related articles: