Operations related to select in JavaScript _ special form Effects

Source: Internet
Author: User
JavaScript reference code for select-related operations. 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 for comparison.

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.