Common Methods for Javascript operation drop-down box

Source: Internet
Author: User

The help house (www.bkjia.com) Tutorial Project encountered the need to use javascript to operate the drop-down box, by the way, make a summary, list some common methods, the following methods have been tested on FIRFOX3.5 and IE8, if other browsers cannot run properly, contact the author.

Reference content is as follows:
// Add a drop-down list
Function AddDropDownList (id, fatherCtl)
{
If (! Document. getElementById (id ))
{
Var ddl = document. createElement ('select ');
Ddl. setAttribute ("id", id );
If (fatherCtl & document. getElementById (fatherCtl ))
Document. getElementById (fatherCtl). appendChild (ddl );
Else
Document. body. appendChild (ddl );
}
}
// Delete the specified drop-down list
Function RemoveDropDownList (id)
{
Var ctl = document. getElementById (id );
If (ctl)
Ctl. parentNode. removeChild (ctl );
}
// Add options to the drop-down list
Function AddDDDLOption (id, text, value)
{
Var ctl = document. getElementById (id );
If (ctl)
{
Ctl. options [ctl. options. length] = new Option (text, value );
}
}
// Delete all options
Function RemoveAllDDLOptions (id)
{
Var ctl = document. getElementById (id );
If (ctl)
{
Ctl. options. length = 0;
}
}
// Delete the option of the specified index
Function RemoveDDLOption (id, index)
{
Var ctl = document. getElementById (id );
If (ctl & ctl. options [index])
{
Ctl. options [index] = null;
}
}
// Obtain the value selected from the drop-down list
Function GetDDLSelectedValue (id)
{
Var ctl = document. getElementById (id );
If (ctl)
{
Return ctl. options [ctl. selectedIndex]. value;
}
}
// Obtain the selected text from the drop-down list
Function GetDDLSelectedText (id)
{
Var ctl = document. getElementById (id );
If (ctl)
{
Return ctl. options [ctl. selectedIndex]. text;
}
}

From: http://www.cnblogs.com/liuwu/ Author: Liu Wu

Related Article

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.