Be careful when using the select Length attribute

Source: Internet
Author: User

This article focuses on two points:
1. When select has only one or more values, length represents different meanings.
2. How to Make length unique.

Let's take a look at two examples.
Example 1:
<P>
<Select size = "1" name = "d1">
<Option> sfdsf </option>
<Option> ghjgfhfg </option>
<Option> qwewqe </option>
</SELECT>
</P>
<P>
<Button name = "B3" onclick = "alert (d1.length)"> click me </button>
</P>

Example 2:
<P>
<Select size = "1" name = "d1">
<Option> sfdsf </option>
<Option> ghjgfhfg </option>
<Option> qwewqe </option>
</SELECT>
<Select size = "1" name = "d1">
<Option> sfdsf </option>
<Option> ghjgfhfg </option>
<Option> qwewqe </option>
</SELECT> </P>
<P>
<Button name = "B3" onclick = "alert (d1.length)"> click me </button> </P>

Click the two buttons in Example 1 and Example 2. Alert has two different results: Example 1 is 3, Example 2 is 2, and d1.length is the same. Why?
This is what I want to talk about:
1. Length indicates different meanings.
When select has only one option, its length indicates the number of options.
When multiple select statements exist, its length indicates the length of the select array.

2. How to make it unique?
Let's make some changes to the two examples above:
Example 1:
<P> <select size = "1" name = "d1">
<Option> sfdsf </option>
<Option> ghjgfhfg </option>
<Option> qwewqe </option>
</SELECT> <p>
<P> <button name = "B3" onclick = "alert (document. getelementsbyname ('d1 '). Length)"> click me </button> </P>

Example 2:
<P> <select size = "1" name = "d1">
<Option> sfdsf </option>
<Option> ghjgfhfg </option>
<Option> qwewqe </option>
</SELECT> <select size = "1" name = "d1">
<Option> sfdsf </option>
<Option> ghjgfhfg </option>
<Option> qwewqe </option>
</SELECT> </P>
<P> <button name = "B3" onclick = "alert (document. getelementsbyname ('d1 '). Length)"> click me </button> </P>

Now let's try again and see if the length points to the select length?
So how to obtain the number of options?
Write as follows:
Oele = Document. getelementsbyname ('1 ')
Optionscount = oele [0]. Options. Length

Conclusion:
When we cannot predict the number of select statements with the same name or ID, we use the document. getelementsbyname (). length method to obtain the select length.

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.