The javascript list box is left and right, compatible with IE and ff. Useful

Source: Internet
Author: User

<HTML>
<Head>
<Title> move the Javascript list box left and right </title>
<Style type = "text/CSS">
Select {width: 100px; Height: 100px ;}
</Style>
<SCRIPT type = "text/JavaScript">
Function roveright (){
VaR lbox = Document. getelementbyid ("leftbox ");
VaR rbox = Document. getelementbyid ("rightbox ");
VaR COUNT = 0;

For (I = 0; I <lbox. length; I ++ ){
If (lbox [I]. Selected ){
// Add an option: New Option (text, value)
Rbox. Options. Add (New Option (lbox [I]. Text, lbox. Value ));
Count ++;
}
}
// The loop is used to delete multiple options at the same time.
For (I = 0; I <count; I ++ ){
Lbox. Remove (lbox. selectedindex );
}
}
Function roveleft (){
VaR lbox = Document. getelementbyid ("leftbox ");
VaR rbox = Document. getelementbyid ("rightbox ");
VaR COUNT = 0;

For (I = 0; I <rbox. length; I ++ ){
If (rbox [I]. Selected ){
Lbox. Options. Add (New Option (rbox [I]. Text, rbox. Vale ));
Count ++;
}
}
For (I = 0; I <count; I ++ ){
Rbox. Remove (rbox. selectedindex );
}
}

</SCRIPT>
</Head>
<Body>
<Select id = "leftbox" multiple = "multiple">
<Option value = "Beijing"> Beijing </option>
<Option value = "Shanghai"> Shanghai </option>
<Option value = "Guangzhou"> Guangzhou </option>
<Option value = "Shenzhen"> Shenzhen </option>
</SELECT>
<Input type = "button" id = "addbtn" onclick = "roveright ();" value = ">>>"/>
<Input type = "button" id = "addbtn" onclick = "roveleft ();" value = "<"/>
<Select id = "rightbox" multiple = "multiple">
<Option value = "Chengdu"> Chengdu </option>
<Option value = "Shenzhen"> Shenzhen </option>
<Option value = "Nanjing"> Nanjing </option>
<Option value = "Tianjin"> Tianjin </option>
</SELECT>
</Body>
</Html>

 

A troublesome function previously written:

// Function additem (){
// Var dsource = Document. getelementbyid ("leftbox ");
// Var dresult = Document. getelementbyid ("rightbox ");
// Var selindex = dsource. selectedindex;
//
/// Add an option: New Option (text, value)
// Dresult. Options [dresult. Options. Length] = New Option (dsource. Value, dsource. value );
// If (document. All ){
// Dsource. Options [selindex]. removenode (true );
//}
// Else {
// Dsource. removechild (dsource. childnodes [selindex]);
//}
//}

 

Because Firefox does not support removenode, You need to determine whether the browser should use different ones. Firefox should use removechild ()

 

/* Improved OO method recently */

<HTML>
<Head>
<Title> move the class left and right in the Javascript list box </title>
<Style type = "text/CSS">
Select {width: 100px; Height: 100px ;}
</Style>
</Head>
<Body>
<Select id = "leftbox" multiple = "multiple">
<Option value = "Beijing"> Beijing </option>
<Option value = "Shanghai"> Shanghai </option>
<Option value = "Guangzhou"> Guangzhou </option>
<Option value = "Shenzhen"> Shenzhen </option>
</SELECT>
<Input type = "button" id = "goright" value = ">>>"/>
<Input type = "button" id = "goleft" value = "<"/>
<Select id = "rightbox" multiple = "multiple">
<Option value = "Chengdu"> Chengdu </option>
<Option value = "Shenzhen"> Shenzhen </option>
<Option value = "Nanjing"> Nanjing </option>
<Option value = "Tianjin"> Tianjin </option>
</SELECT>
<SCRIPT type = "text/JavaScript">
// Constructor
Function moveselect (selector ){
This. S1 = selector. select1;
This. S2 = selector. select2;
This. T1 = selector. button1;
This. T2 = selector. button2;
// This. Move (); // used directly after instantiation
}

// Use the prototype Extension Method
Moveselect. Prototype = {
Move: function (){
VaR _ Self = this;
_ Self. t1.onclick = function (){
VaR COUNT = 0;
For (VAR I = 0; I <_ Self. s1.length; I ++ ){
If (_ Self. S1 [I]. Selected ){
_ Self. s2.options. Add (New Option (_ Self. S1 [I]. Text, _ Self. S1 [I]. Value ));
Count ++;
}
}
For (var s = 0; S <count; s ++ ){
_ Self. s1.remove (_ Self. s1.selectedindex );
}
}
_ Self. t2.onclick = function (){
VaR COUNT = 0;
For (VAR I = 0; I <_ Self. s2.length; I ++ ){
If (_ Self. S2 [I]. Selected ){
_ Self. s1.options. Add (New Option (_ Self. S2 [I]. Text, _ Self. S2 [I]. Value ));
Count ++;
}
}
For (var s = 0; S <count; s ++ ){
_ Self. s2.remove (_ Self. s2.selectedindex );
}
}
}
}

// Call
VaR selement = {
Select1: Document. getelementbyid ("leftbox "),
Select2: Document. getelementbyid ("rightbox "),
Button1: Document. getelementbyid ("goright "),
Button2: Document. getelementbyid ("goleft ")
};
VaR M1 = new moveselect (selement );
M1.move ();
</SCRIPT>
</Body>
</Html>


Javascript ListBox move around
BeijingShanghaiGuangzhouShenzhen


ChengduShenzhenNanjingTianjin

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.