document.getElementsByName
document.getElementsByName can find out a specific set of Name objects in an HTML file. The usage is similar to document.getElementById , but because there may be more than one Name in the HTML file, the returned value of document.getElementsByName is not a single Name value. Instead, it returns a set of JavaScript Array array elements, each array value is document.getElementsByName("Name")[i] This way, but getElementsByName has been cancelled in DOM Level 3, and you can also use it if you want to get HTML elements document.getElementById or document.getElementsByTagName etc.
Document.getElementsByName basic syntax
document.getElementsByName( name )
Simply put, simply put the name to be read into the function, although document.getElementsByName has been cancelled, but it can still be used for some applications, such as counting the total number of a certain name element in the HTML file. How much, please refer to the following example.
document.getElementsByName example
function GoCountNum(){
alert(document.getElementsByName("test").length);
}
</script>
<input name="test" type="text"><br>
<input name="test" type="text"><br>
<input name="test" type="text"><br><br>
<input type="button" onclick="GoCountNum()" value="計算欄位數量">

In the example, we have prepared three HTML input text text input fields and an HTML button button . The name="test" object set of this HTML document is obtained through document.getElementsByName , and the array length is displayed through the dialog window using alert . The answer to this example is 3. There are three sets of name="test" objects.
Post a Comment
0 Comments