JavascriptgetElementsByName ()

Source: Internet
Author: User
When there are multiple widgets with the same name on the page, what do you do first? Determine the length? Indeed, from the strict perspective of the program, we need to judge the length, and there are two reference methods: length and no length. Let's look at this:
OEle = document. all. aaa; // There is an aaa object, but we don't know how long it is, so we can't operate on it. therefore, we need to judge the length first. as follows:
If (oEle. length) {} else {};
In either case, the content in curly braces is written differently:

The Code is as follows:


If (oEle. length ){
For (var I = 0; I OEle [I]. value ........
}
}
Else {
OEle. value ........
};


But is writing like this too complicated? When there are many codes in curly braces, We need to write the code twice ~
Fortunately, there is the document. getElementsByName () method. It processes the same for one or more objects. We can use:
OEle = document. getElementsByName ('aaa') to reference
When there is only one oEle, It is oEle [0]. When there are multiple oEle, it is very easy to use the subscript method oEle [I] to obtain them cyclically?
It is worth mentioning that it is equally valid for Name and ID.
However, it can only be applied to document objects. There is another method to apply a wider range of objects:
GetElementsByTagName, for example ......
I want to take all the input in the DIV, so that I can write it: aaa. getElementsByTagName ('input'), so that it is effective and can be different from other DIV (for example, there is a DIV called bbb, which contains the same INPUT.
Corresponds to getElementsByTagName, and there is a document. body. all. tags (). The objects using this method are much smaller than getElementsByTagName, but more than getElementsByName.
At this point, we also need to mention getElementById, which can only be used by the document Object and returns the first element of the array. Haha, its method names are all written as getElement rather than getElements. Therefore, never be confused.
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.