"Dual ListBox" implemented with Javascript (bi-directional selector)

Source: Internet
Author: User
Javascript

This is an application example of the "Dual ListBox" (a two-way selector) I made out of Javascript, and it was extracted from my code. Often used in web programming.

Perhaps my implementation is too cumbersome, I hope you have better code to contribute out.

<title> selector </title>
<link href= "./style/style.css" rel= "stylesheet" type= "Text/css" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<meta http-equiv= "Page-enter" content= "Blendtrans (duration=1.0)" >
<meta http-equiv= "Page-exit" content= "Blendtrans (duration=1.0)" >
<script language= "JavaScript" >
function Openwin (URL, L, T, W, h)
{Open (URL, ', ' toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width= ' + w + ', height= ' + H + ', Left= ' + L + ', top= ' + t);}

function Check_and_submit (frm)
{
SelectAll (frm. SelectedItem);
Frm.submit ();
}
</script>

<body>

<form name= "Frm1" id= "Frm1" method= "post" action= "save.asp" >
<input name= "Allowsubmit" type= "hidden" value= "OK" >

<table width= "border=" 0 "cellspacing=" 0 "cellpadding=" 0 "align=" center ">
<TR HEIGHT=10><TD colspan=3></td></tr>
<tr>
<TD width= "Align=center" valign= "Top" >
Columns assigned to this user management:<br><br>
<select name= "SelectedItem" id= "SelectedItem" size=12 multiple= "true" >
<option> No </option>

</select>
</select>
</td>
&LT;TD width= "Align=center>"
<br><br>
<button ><</button><br><br>
<button ><<</button><br><br><br><br>
<button >></button><br><br>
<button >>></button><br>
</td>
&LT;TD width= "Align=center" valign= "Top" >
Columns to be allocated:<br><br>
<select name= "Waitselectitem" size=12 multiple=true>
<option> Normal News </option>
<option> Video News </option>
<option> Photo News </option>
<option> Special News </option>
<option> 100,000 Why </option>
<option> Code Testing </option>
<option>www.why100000.com</option>
</select>
</td>
</tr>
</table>
</form>

<script language= "JavaScript" >
function Movesingleitem (Sel_source, Sel_dest)
{
if (sel_source.selectedindex==-1)//Source: No Items selected
Return

if (sel_source.options[0].text== "none")//Source: Only "None" item
Return

if (sel_dest.options[0].text== "none")//target: Only "None" item, delete the prompt item first
Sel_dest.options.remove (0);

var selectedtext = Sel_source.options[sel_source.selectedindex].text;
Sel_dest.options.add (New Option (SelectedText));
Sel_source.options.remove (Sel_source.selectedindex);

if (sel_source.options.length==0)//Source: If all useful items have been deleted, add the hint item: "None"
Sel_source.options.add (New Option ("none"));
}

function Moveallitems (Sel_source, Sel_dest)
{
if (sel_source.options[0].text== "none")//Source: Only "None" item
Return

if (sel_dest.options[0].text== "none")//target: Only "None" item, delete the prompt item first
Sel_dest.options.remove (0);

First copy all items to the target:
var sel_source_len = sel_source.length;
for (var j=0; j<sel_source_len; J + +)
{
var selectedtext = Sel_source.options[j].text;
Sel_dest.options.add (New Option (SelectedText));
}

Then delete all items from the source:
while ((k=sel_source.length-1) >=0)
{
if (sel_source.options[0].text== "none")//Source: Only "None" item
Break
Sel_source.options.remove (k);
if (sel_source.options.length==0)//Source: If all useful items have been deleted, add the hint item: "None"
Sel_source.options.add (New Option ("none"));
}
}

function SelectAll (Thesel)//Select all items in select
{for (i = 0; i<thesel.length;i++)
Thesel.options[i].selected = true;
}
</script>



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.