Comparison of 3 Methods to Get HTML Elements

Comparison of 3 methods for obtaining HTML elements: document.getElementsByName(), document.getElementById(), document.getElementsByTagName()

 

document.getElementsByName() is to obtain the control object according to the control name, because the control names can be the same, and an array of objects is returned. 

document.getElementById() obtains the control object according to the control ID, because the control ID is unique, and an object is returned. 
document.getElementsByTagName() obtains the control object according to the control TAG, and returns an object array. 
For example: 
There are controls in HTML as follows: 
    <input type="radio" id="radio1" checked="checked" name="radiogroup1" value="1"/> 
    <input type="radio" id="radio2" name ="radiogroup1" value="2"/> 
    <input type="radio" id="radio3" name="radiogroup1" value="3"/> 
then: 
 document.getElementsByName('radiogroup1') gets these three A control, you can use document.getElementsByName('radiogroup1')[0] to get the first control. 
 document.getElementById('radio1') gets the first control, 
 document.





getElementsByName('delID') returns an array of all controls named 'delID', and an array is a collection.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326608937&siteId=291194637