Clear option values in select using JavaScript

Source: Internet
Author: User

We sometimes need to clear the values in the options set in the select control,
I found it online, most of which are
OBJ = Document. getelementbyid ("DRP"); // DRP is the ID value of the select control.
For (I = 0; I <obj. Options. length; I ++)
OBJ. Options [I] = NULL;

However, this method does not seem to be able to complete the clearing operation.
I tested it several times. If I had the numbers from 0 to 8, after I ran a cleanup operation, I found that 1, 3, 5, and 7 still exist. If I click "clear" again, only 3, 7 exists. Reduce by half each time.
At first, I couldn't figure out why it was like this. Later I thought it might be like this:
Because obj. Options. length is decreased along with the user's obj. Options [I] = NULL; operation.
So when the fourth row is deleted, I = 4 and obj. Options. length are also equal to 4, so the loop is exited, so that only half is deleted.

Therefore, the correct method is to delete from the back.
OBJ = Document. getelementbyid ("DRP"); // DRP is the ID value of the select control.
For (I = obj. Options. Length-1; I> = 0; I --)
OBJ. Options [I] = NULL;

In this way, you can clear

Another method should be to replace obj. Options. length in the for loop with a variable and assign a value to this variable before the loop.

Javascript is really inconvenient. In. net, the clear method of the Set object is usually called, but it is so troublesome here.

We sometimes need to clear the values in the options set in the select control,
I found it online, most of which are
OBJ = Document. getelementbyid ("DRP"); // DRP is the ID value of the select control.
For (I = 0; I <obj. Options. length; I ++)
OBJ. Options [I] = NULL;

However, this method does not seem to be able to complete the clearing operation.
I tested it several times. If I had the numbers from 0 to 8, after I ran a cleanup operation, I found that 1, 3, 5, and 7 still exist. If I click "clear" again, only 3, 7 exists. Reduce by half each time.
At first, I couldn't figure out why it was like this. Later I thought it might be like this:
Because obj. Options. length is decreased along with the user's obj. Options [I] = NULL; operation.
So when the fourth row is deleted, I = 4 and obj. Options. length are also equal to 4, so the loop is exited, so that only half is deleted.

Therefore, the correct method is to delete from the back.
OBJ = Document. getelementbyid ("DRP"); // DRP is the ID value of the select control.
For (I = obj. Options. Length-1; I> = 0; I --)
OBJ. Options [I] = NULL;

In this way, you can clear

Another method should be to replace obj. Options. length in the for loop with a variable and assign a value to this variable before the loop.

Javascript is really inconvenient. In. net, the clear method of the Set object is usually called, but it is so troublesome here.

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.