The getElementById () method of the html DOM document Object, domgetelementbyid

Source: Internet
Author: User

The getElementById () method of the html DOM document Object, domgetelementbyid

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

Syntax:

document.getElementById(id)

The html dom defines multiple methods for searching elements. Besides getElementById (), getElementsByName () and getElementsByTagName ().

However, if you need to find a specific element in the document, the most effective method is getElementById ().

When operating on a specific element of a document, it is best to give the element an id attribute to specify a unique name (in the document) for it, then you can use this ID to find the desired element.

Example:

document.getElementById("myHeader")  alert(x.innerHTML)  }</script>

Example: getElementById () is an important method. It is very common in DOM programming. The following defines a tool function, so that you can use the getElementById () method with a short name:

function id(x) {  if (typeof x == "string") return document.getElementById(x);  return x;  }

The above function accepts element IDS as their parameters. For each such parameter, you only need to write x = id (x) before use.

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.