Common Methods for Javascript select drop-down box operations

Source: Internet
Author: User

Copy codeThe Code is as follows:
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;
}
}

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.