Link to home
Start Free TrialLog in
Avatar of avivak1
avivak1

asked on

Select from a listbox- move selected item to another listbox

Hi,

Does anyone out there have any code, or can direct me to the following senario.

I would like 2 listboxes.

One shows a list of users who have no access rights. Another is a listbox of who does.

If I double click on one item in one list it should jump to the other and vice versa.

If I choose the > arrow it should move selected item from left listbox to right
If I choose the < arrow it should move the selected item from right listbox to left

If I choose the >> arrow it should move all users from left listbox to right
If I choose the << arrow it should move all the users from right to left.

Thanks and here's a picture just to be sure

---------------------------------------------          ---------------------------------------------
-      User A                                      -    >   -         User B                                    -
-      User C                                      -         -          User E                                   -
-      User D                                      -    <   -                                                      -
-      User F                                       -         -                                                      -
-                                                      -  >>   -                                                      -
-                                                      -         -                                                      -
-                                                      -  <<   -                                                      -
---------------------------------------------         -----------------------------------------------

Avatar of sajuks
sajuks

// try this
<script language="JavaScript">

function dosubmit() {}

function deleteOption(object,index) {
object.options[index] = null;
}

function addOption(object,text,value) {
var defaultSelected = true;
var selected = true;
var optionName = new Option(text, value, defaultSelected, selected)
object.options[object.length] = optionName;
}

function copySelected(fromObject,toObject) {
for (var i=0, l=fromObject.options.length;i<l;i++) {
if (fromObject.options[i].selected)
addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
}
for (var i=fromObject.options.length-1;i>-1;i--) {
if (fromObject.options[i].selected)
deleteOption(fromObject,i);
}
}

function copyAll(fromObject,toObject) {
for (var i=0, l=fromObject.options.length;i<l;i++) {
addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
}
for (var i=fromObject.options.length-1;i>-1;i--) {
deleteOption(fromObject,i);
}
}
</script>

<html>

<head>
<title></title>
</head>

<body>

<form>
  <table>
    <tr>
      <td><select name="select1" multiple size="7">
        <option>User A </option>
        <option>User B</option>
        <option>User C</option>
        <option>User D </option>
        <option>User E</option>
        <option>User F</option>

      </select> </td>
      <td><input type="button" value=" &gt; "
      onClick="copySelected(this.form.select1,this.form.select2), dosubmit(select2)"> <p><input
      type="button" value=" &lt; "
      onClick="copySelected(this.form.select2,this.form.select1), dosubmit(select2)"> </p>
      <p><input type="button" value="&gt;&gt;"
      onClick="if (document.images) copyAll(this.form.select1,this.form.select2)"> </p>
      <p><input type="button" value="&lt;&lt;"
      onClick="if (document.images) copyAll(this.form.select2,this.form.select1)"> </td>
      <td><select name="select2" multiple size="7">
      </select> </td>
    </tr>
  </table>
</form>
</body>
</html>
Avatar of avivak1

ASKER

Perfect, except for one thing.

I would like to double click on any of the options and it will go to the opposite box.

Thanks,
Aviva
ASKER CERTIFIED SOLUTION
Avatar of Webby_
Webby_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of avivak1

ASKER

I have a weird senario. Maybe you can help?

The first time I choose the options and click submit - they get entered into a table - no problem.

If I go back to the page with the choices in the list box - with the list box populated with the data that was entered If I click submit - the Request.Form does not recognise the values that are in the list

Review:
I put in User A, User B and User F into the Access List - works
When I come back
User A, B, and F are in the Access List Box

When I click submit Request.Form("Select2") has no data inside. (Using ASP etc)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of avivak1

ASKER

No that doesn't fix the issue. (but thanks!)

Also. There is no order so everything jumps all over the place depending on the different clicks
Hm, it's hard to tell without the source, but I'm guessing that when you're populating Select2 from your table/db, you're not assigning values to the options.  A quick way to check would be to put A, B, and F into the Access List and submit.  Return to the page, and then View Source on your browser.  The options in Select2 should all have an attribute that says "value=SOMETHING" - if they don't, then either the ASP isn't populating the fields correctly, or the values aren't being stored into the table in the first place (maybe even both).  Let me know if that doesn't make sense, and I'll try to explain.  Hope that helps.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of avivak1

ASKER

Thanks for all your help

Webby's answer was closest and of course mcouillard it's nice to see other options