HTML DOM getElementById () method

Source: Internet
Author: User

Definition and usage

The getElementById () method returns a reference to the first object that owns the specified ID.

Grammar
document.getElementById (ID)
Description

The HTML DOM defines a variety of methods for finding elements, with the exception of getElementById () and Getelementsbyname () and getElementsByTagName ().

However, if you need to find a particular element in the document, the most efficient way is getElementById ().

When manipulating a particular element of a document, it is best to give the element an id attribute, assign it a unique name (in the document), and then use that ID to find the element you want.

Example 1
document.getElementById("myHeader")  Alert ( x.innerhtml)  }</script>
Example 2

getElementById () is an important method that is used very often in DOM programming. We have defined a tool function for you so that you can use the getElementById () method with a shorter name:

function ID (x) {  document.getElementById(x) ;  return x;  }

The above function accepts the element ID as their argument. For each of these parameters, you can just write X = ID (x) before using it.

HTML DOM getElementById () method

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.