Name can be used in the document.getElementsByName() method, a form and with the form elements collection. Var n = document.getElementsByName(linkName) ĭocument.getElementById("demo").Note: The name property doesn't exist for other elements unlike tagName and nodeName, it is not a property of the Node, Element or HTMLElement interfaces. Var linkName = prompt("Please enter the link names a or b which you want to see", "") Click below button to count the number of links available in this document by getElementByName() function.ĭocument.getElementById("txt").innerHTML ="The total number of links are : "+n.length Some of the important information about the india like 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 and display the specified link according to the order of link present in the document, as in the following example: Output: similarly, we click on the “Count IT related Links” button, the output is:Įxplanation: As in the above code the document contains some links by two names as India and IT to print the number of specific links the buttons are created, once we click the specific button is called to the specific countIn() or countIT() function inside where it called to document.getElementsByName(“India”) or document.getElementsByName(“IT”) function which returns the array of all elements and then the total number of the specific links available in the document is printing. Output: Once we click on the “Count India related Links” button, the output is: Var n = document.getElementsByName("IT") ĭocument.getElementById("txt").innerHTML ="The total number of links for the IT are : "+n.length Var n = document.getElementsByName("India") ĭocument.getElementById("txt").innerHTML ="The total number of links for the India are : "+n.length Click below button to count the number of specific links available in this document by getElementByName() function. Some of the important information about the india and IT like 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 Next, we write the html code to understand the getElementsByName() function more clearly with the example where we will use getElementsByName() function to count the number of list elements available in the document by specifying “li” tag name in this function, as in the following example – onion tomato potato Example #1 How getElementsByName() work in JavaScript? The returned array stored elements in the sorted order if they are present in the document. The return value of this function is an array of collection of all html elements which match by the given name.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |