How to Returns the element with the specified ID, or name, in an HTMLCollection in JavaScript ?
To return the element with the specified ID or name in an HTMLCollection in JavaScript, you can use the following methods:
- getElementById() method:
The
getElementById()
method returns the element that has the ID attribute with the specified value. This method is used to get a single element by its ID.
Example:
let element = document.getElementById("myId");
- getElementsByName() method:
The
getElementsByName()
method returns a collection of all elements in the document with the specified name attribute. This method is used to get a collection of elements by their name.
Example:
let elements = document.getElementsByName("myName");
Note: Both of these methods return a single element or a collection of elements that match the specified ID or name. If no elements are found, they return null or an empty collection.