getElementById ()

Source: Internet
Author: User

getElementById (): The purpose of the method is to look for an element with a given id attribute worth:

element = document.getElementById (ID);

This method returns a node with the given ID attribute that is worth the element. If such an element does not exist, he returns null. This method can only be used for document objects.

The element node returned by the getElementById () method is an object that has properties such as NodeName, NodeType, ParentNode, Childnode, and so on.

The following example extracts the element with the ID zodiac value as FinePrint and saves it in the message variable. The parent node of the message element node is also an element, which we extract into the variable container:

var message = document.getElementById ("FinePrint"); var container = Message.parentnode;

If you are going to extract an element with an ID, then retrieving it with the getElementById () method will be the simplest and quickest method, and once you find this element, you can use SetAttribute (), CloneNode (), AppendChild (), and so on.

In the following example, we first find out that the id attribute value is an element of fineprint, save it to the message variable, and then refresh the title property of this element to this is important

var message = document.getElementById ("FinePrint"); Message.setattribute ("title", "This is imortant ");

In the same document, the ID attribute values of the different elements must vary, and if more than one element has the same Id,getelementbyid () method The behavior will not be expected.

getElementById ()

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.