Operations related to select in Javascript

Source: Internet
Author: User

I. Insert Option
1. DOM method
VaR oselectyear = Document. getelementbyid ("selectyear ");
VaR op = Document. createelement ("option ");
Op. innerhtml = "2010 ";
Op. value = "2010 ";
Oselectyear. appendchild (OP );

2. New Option Method
VaR oselectmonth = Document. getelementbyid ("selectmonth ");
Oselectmonth. Options. Add (New Option (1, 1 ));
Oselectmonth. Options. Add (New Option (2, 2 ));

Ii. Clear Option
VaR oselectmonth = Document. getelementbyid ("selectmonth ");
Oselectmonth. Options. Length = 0; // clear options in select

3. Set the default option
var oselectmonth = document. getelementbyid ("selectmonth");
// oselectmonth. selectedindex = 1; // Method 1: The second item is selected by default
// setTimeout (function () {oselectmonth. selectedindex = 1 ;}, 0); // the setTimeout delay is used to prevent Dom rendering problems
// oselectmonth. options [1]. selected = true; // method 2
oselectmonth. options [1]. setattribute ("selected", "true"); // method 3: setattribute is recommended.

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.