JS Operation select dropdown box dynamic change (Create/delete/get) _javascript tips

Source: Internet
Author: User
1. Create a Select dynamically
Copy Code code as follows:

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

2. Add Options option
Copy Code code as follows:

function AddOption () {
To find an object based on its ID,
var obj=document.getelementbyidx_x (' Myselect ');
Add an option
Obj.add (New Option ("text", "value");
}

3. Delete all options option
Copy Code code as follows:

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

4. Delete an option
Copy Code code as follows:

function Removeone () {
var obj=document.getelementbyidx_x (' Myselect ');
Index, to delete the ordinal number of the option, take the number of the currently selected option
var Index=obj.selectedindex;
Obj.options.remove (index);
}

5. Get the value of options option
Copy Code code as follows:

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

6. Get Options Option text
Copy Code code as follows:

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

7. Modify Options option
Copy Code code as follows:

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

8. Delete Select
Copy Code code as follows:

function Removeselect () {
var myselect = document.getelementbyidx_x ("Myselect");
MySelect.parentNode.removeChild (Myselect);
}

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.