Last Updated : Apr 9, 2023
The name attribute permits more HTML components to share the same value as the id attribute does.
You can use the document object's getElementsByName() method to retrieve all items with a given name.
The function getElementsByName() accepts a name, which is the value of an element's name property, and returns a live NodeList of the element's elements.
The elements return collection is operational. It implies that whenever items that have the same name are added to it or excluded from the document, its return elements are instantly updated.
The return value of the getElementsByName() method is a collection of all the elements of a specific document organised by name.
This group of items is known as a node list, and the index can be used to browse over each node list component.
The document name is accepted by this function. Further collections of elements are returned by this function.
- Using the HTML and Head tags, we begin our code. Here, we use the TITLE element to construct the page's title.
- Finally, using the STYLE tag, we implement the CSS stylesheet, using the h1 element's green colour and the body element's text alignment as a centre.
- After that, the style is finished, and the script is started.
- To display things at a specific name, we construct a function in script called tk.
- To print the result entered by the user, we then utilise the getelementbyname function with the alert method.
- After that, we terminate the script and use the BODY tag to begin the code body.
- We write some statements in the body of the code using the H1 tag and some other statements using the H2 tag.
- Next, we write the text using the input type method.
- Next, we build the button with the onclick event, which will trigger the function when the user presses the button.
- The BODY and HTML elements are where our code concludes.
We also discovered that the getElementsByName() method of the document object may be used to fetch all objects with a specific name as well as the function getElementsByName() receives a name, the value of the name attribute of an element, and gives a real NodeList of an item's elements.
The collection of returned elements is operational.