Sample Code of adding, deleting, modifying, and querying the option of the select control in JS

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to learn about the Javascript operation select, which is a common form, the following describes several common methods for performing select operations in javascript:
// JavaScript Document

// Dynamically create select
Function createSelect ()
{
Var mySelect = document. createElement ("select ");
MySelect. id = "mySelect ";
Document. body. appendChild (mySelect );
}

// Add option
Function addOption ()
{
// Search for objects by id,
Var obj = document. getElementById ('myselect ');
// Add an option
Obj. add (new Option ("text", "value"); // This can only be valid in IE
Obj. options. add (new Option ("text", "value"); // compatible with IE and firefox
}


// Delete all option options
Function removeAll ()
{
Var obj = document. getElementById ('myselect ');
Obj. options. length = 0;
}


// Delete an option
Function removeOne ()
{
Var obj = document. getElementById ('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 );
}

// Obtain the option text
Var obj = document. getElementById ('myselect ');
Var index = obj. selectedIndex; // the serial number of the selected option.
Var val = obj. options [index]. text;

// Modify option
Var obj = document. getElementById ('myselect ');
Var index = obj. selectedIndex; // the serial number of the selected option.
Var val = obj. options [index] = new Option ("new text", "new Value ");

Select.zip (675 B download: 33 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.