Get page element values with JavaScript

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.