Home >Web Front-end >JS Tutorial >Document:getElementsByName() usage methods and examples_javascript skills

Document:getElementsByName() usage methods and examples_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:18:251551browse
Definition and Usage

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 the name attribute of the element instead of the id attribute.

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

Example
Copy code The code is as follows:

















getElementById, which can only be used by document objects , and what is returned is the first element of the array. Haha, its method name states that it is getElement instead of getElements, so don’t get confused. getElementsByName returns all elements whose names are the specified values. Collection

"Gets a collection of objects based on the value of the NAME tag attribute." Collections are much looser than arrays. The type of each item in the collection can be different. Collections just put certain elements together as a category To use, in contrast, arrays are much stricter, each sub-item is of a unified type. The results obtained by methods such as document.getElementsByName, document.getElementsByTagName, document.formName.elements are all collections. Used for batch updates time data transfer.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn