JS operation select drop-down box dynamic change

Source: Internet
Author: User

1. dynamically create select

   Function createSelect (){

       Var mySelect = document. createElement_x ("select ");

       MySelect. id = "mySelect "; 

       Document. body. appendChild (mySelect );

   }

2. Add option

  Function addOption (){

       // Search for objects by id,

        Var obj = document. getElementByIdx_x ('myselect ');

        // Add an option

        Obj. add (new  Option ("text", "value "));  

  }

3. Delete All option options

  Function removeAll (){

        Var obj = document. getElementByIdx_x ('myselect ');

  

        Obj. options. length = 0; 

  }

4. delete an option

Function removeOne (){

        Var obj = document. getElementByIdx_x ('myselect ');

        // Index. The sequence number of the option to be deleted. The sequence number of the selected option is used here.

        Var index = obj. selectedIndex;

        Obj. options. remove (index ); 

  }

5. Obtain the option value.

Var obj = document. getElementByIdx_x ('myselect ');

Var index = obj. selectedIndex; // the serial number of the selected option.

Var val = obj. options [index]. value;

6. Obtain the option text

Var obj = document. getElementByIdx_x ('myselect ');

Var index = obj. selectedIndex; // the serial number of the selected option.

Var val = obj. options [index]. text;

7. Modify option

Var obj = document. getElementByIdx_x ('myselect ');

Var index = obj. selectedIndex; // the serial number of the selected option.

Var val = obj. options [index] = new Option ("new text", "new Value ");

8. Delete select

   Function removeSelect (){

         Var mySelect = document. getElementByIdx_x ("mySelect ");

        MySelect. parentNode. removeChild (mySelect );

  }

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.