There are three common ways to get page elements using JavaScript :
getElementById ()
Getelementsbyname ()
getElementsByTagName ()
The syntax for the "document.+ method name" obtains its page elements, which can be manipulated after the attribute is added.
such as document.getElementById ("ID name") is based on the page ID gets the element, in one page the element ID must be only one by one, otherwise this method will not take its elements. ID is equivalent to our personal ID number, in a world where everyone is the only ID number. If there is no such ID in the page, you will not find the element you are looking for and will report a null error.
For example: Var X=document.getelementbyid ("id"). value;
With Getelementsbyname () This method is based on the page element name to get the page element, in a page, the element ID is unique, but the page element name name can be duplicated, name, such as our name, in this world, there will be duplicate names exist. Suppose a two people named Tom together, the other people come to Tom, will directly find two, in the same way in the page, we will get an array. If we find a specific page element, we can add a subscript "[integer]" After this method, and the subscript is starting from 0, which is the position of the element you want to get in the page minus one.
For example: var x = document.getelementsbyname ("ABCD"). Value; The x at this point is an array of all the Name= "ABCD" in the page.
var x = document.getelementsbyname ("ABCD") [0].value; The x at this point is the value of the first name= ' ABCD ' in the page
Using getElementsByTagName () This method is obtained based on the HTML tag. Because the label used by the page element can be duplicated, the value of this method is also an array. If we want to get the value of a specific element in this way, it can be expressed directly in the subscript, the beginning of the subscript is also starting from 0, of course, you can also first to the array, in the array to get.
For example: var x = document.getElementsByTagName ("P") [2].value; The x at this point is the value of the third paragraph label in the page
var x = document.getElementsByTagName ("P"). Value; At this point, X is an array, which is an array of all <p></p> elements in the page.
Get page element values with JavaScript