

Next, we write the html code to understand the getElementsByName() function where we will use getElementsByName() function to count the number of link elements available in the document by specifying “a” tag name in this function, as in the following example – From here return an array of the total number of “fruits” elements and which is printing as in the above code.

Output: Similarly, if we click on the “Count Both” button, the output is:Įxplanation: As in the above code the list of elements (fruits and vegetables) are created and the button is created to count the list of fruits, vegetables and both available, once we click the specific button is called to the respective count function for example if we click on count Fruits button it calls to the countFun() function inside where it called to document.getElementsByName(“fruits”) function which return the array of all elements by “fruits”.

Output: Click on the “Count vegetables” button, the output is: Output: Once we click on the “Count Fruits” button, the output is: Var m = document.getElementsByName("vegetables") Īlert("The total number of fruits and vegetables are : "+t+".") Var n = document.getElementsByName("vegetables") Īlert("The total number of vegetables are : "+n.length+".") Var n = document.getElementsByName("fruits") Īlert("The total number of fruits are : "+n.length+ ".") Here we are going to count total number of fruits and vegetables by getElementByName() function. This is an example for getElementsByName() function
