On the option object _javascript skill in the Pull-down menu

Source: Internet
Author: User
Tags array length

1. Create option Object

1.1 var optionEle1 = document.createelement (' option ');

1.2 var optionEle2 = new Option (text, value, defaultselected, selected);

2.options Properties

2.1 Select.options Returns a collection of option objects below the Select label

3. Empty the Pull-down menu

3.1 using for loop deletion, note the dynamic change of array length

3.2 select.options.length = 0;

4. Application

 
 

1. Create a Select dynamically

function Createselect () { 
 
    var myselect = document.createelement ("select");  
Myselect.id = "Myselect";  
Document.body.appendChild (Myselect); 
} 

2. Add Options option

function AddOption () { 
 
     //Lookup object based on ID, 
var obj=document.getelementbyid (' Myselect '); 
 
      Add an option 
obj.add (new   option ("Text", "value");  This can only be effective in IE 
obj.options.add (new Option ("text", "value");//This compatible IE and Firefox 
} 

3. Delete all options option

function RemoveAll () { 
var Obj=document.getelementbyid (' Myselect '); 
 
obj.options.length=0; 
 
   } 

4. Delete an option

function Removeone () { 
var Obj=document.getelementbyid (' Myselect '); 
 
      Index, to delete the number of options, here take the current selection of the serial number 
 
    var index=obj.selectedindex; 
Obj.options.remove (index);  
} 

5. Get the value of options option

var Obj=document.getelementbyid (' Myselect '); 
 
var Index=obj.selectedindex; Ordinal number, taking the ordinal number of the currently selected option 
 
var val = obj.options[index].value; 

6. Get Options Option text

var Obj=document.getelementbyid (' Myselect '); 
 
var Index=obj.selectedindex; Ordinal number, taking the ordinal number of the currently selected option 
 
var val = obj.options[index].text; 

7. Modify Options option

var Obj=document.getelementbyid (' Myselect '); 
 
var Index=obj.selectedindex; Ordinal number, taking the ordinal number of the currently selected option 
 
var val = obj.options[index]=new option ("New text", "New value"); 

8. Delete Select

function Removeselect () { 
var myselect = document.getElementById ("Myselect"); 
MySelect.parentNode.removeChild (Myselect); 
} 
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//ZH-CN" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" > 

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.