Parse the select drop-down list using Javascript

Source: Internet
Author: User

Do you know how to operate the select drop-down list in Javascript? Here we will share with you. I believe this article will surely help you gain some benefits.

Select drop-down list for Javascript operations

The select drop-down table for javascript operations, including reading select content, sequence number, selection value, adding option content, modifying option content, and changing the option order.

View the overall effect: select drop-down list for javascript operations

1. First, access the select option. select defines the set of options, which can be accessed through the Element Set combination.

Example:

 
 
  1. <Selectidselectid = "forasp_cn" name = "forasp_cn">
  2. <Optionvalueoptionvalue = "http://www.forasp.cn/" selected>
  3. Website of website production Learning Network </opiton>
  4. <Optionvalueoptionvalue = "http://www.forasp.cn/yule/">
  5. Website production Learning Network entertainment website </opiton>
  6. <Optionvalueoptionvalue = "http://www.forasp.cn/tool/">
  7. Website creation learning network Tool website </opiton>
  8. </Select>

The js Code is as follows:

 
 
  1. Varforaspobj = document. getElementById ("forasp_cn ")
  2. Varoption_num = foraspobj. options. length;
  3. // Obtain the number of options. Currently, 3 is output.
  4. Obtain the select option in Dom and obtain the displayed text value and value.
  5. Varoption_text1 = foraspobj. options [0]. firstChild. nodeValue;
  6. // Output "website production Learning Network URL"
  7. Varoption_value1 = foraspobj. options [0]. getAttribute ("value ");
  8. // Output "http://www.forasp.cn /"
  9. Obtain the select option display text and value values under Bom.
  10. Varoption_text1 = foraspobj. option [0]. text; // output is the same as above
  11. Varoption_text1 = foraspobj. option [0]. value; // output is the same as above

2. Obtain the text and value values of the currently selected option under select. select also has a special feature selectedIndex, which is the index of the currently selected option.

 
 
  1. Alert (foraspobj. selectedIndex); // output 0
  2. For (I = 0; I <foraspobj. options. length; I ++)
  3. {
  4. If (foraspobj. options [I]. selected)
  5. {
  6. Alert (foraspobj. options [I]. text +
  7. "\ N" + foraspobj. options [I]. value );
  8. // Output "website production Learning Network URL press ENTER http://www.forasp.cn /"
  9. Break;
  10. }
  11. }

3. Modify the value and text of the select option.

Assume that you change the first text display value to http://www.forasp.cn/valuevalue
The Dom modification code is as follows:

 
 
  1. Foraspobj. options [0]. firstChild. nodeValue = "http://www.forasp.cn ";
  2.  
  3. Foraspobj. options [0]. setAttribute ("value", "website production Learning Network ");

The Bom modification code is as follows:

 
 
  1. Foraspobj. options [0]. text = "http://www.forasp.cn ";
  2. Foraspobj. options [0]. value = "website production Learning Network ";

4. Add options. Use the function to add select option options.

T indicates the text value, and v indicates the value.

 
 
  1. Vartemp_option = document. createElement ("option ");
  2. // Create a temporary option
  3. Temp_option.appendChild (document. createTextNode ("new content "));
  4. // Create a subtext node for option
  5. Temp_option.setAttribute ("value", "new value added ");
  6. // Assign a value to opiton
  7. Foraspobj. appendChild (temp_option)
  8. // Append option to the parent class

5. Delete the option. Delete the select option. simply use the corresponding option object to be null.

For example, delete the last option.

 
 
  1. if(foraspobj.options.length>0)  
  2. {foraspobj.options[foraspobj.options.length-1]=null;} 

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.