Copying Selectbox options to another selectbox

Discussion in 'Javascript' started by shankwheat, Nov 29, 2006.

  1. shankwheat

    shankwheat Guest

    I have a form named 'choiceForm' and 2 select boxes named 'available'
    and 'move'. I give users the ability of copying options (not moving)
    from available to move with the code below. I would like to create a
    button which copies all options from available to move but not sure how
    to do this. Thanks for any help.

    function moveOver()
    {
    var boxLength = document.choiceForm.move.length;
    var selectedItem = document.choiceForm.available.selectedIndex;
    var selectedText =
    document.choiceForm.available.options[selectedItem].text;
    var selectedValue =
    document.choiceForm.available.options[selectedItem].value;

    var i;
    var isNew = true;

    if (boxLength != 0) {
    for (i = 0; i < boxLength; i++) {
    thisitem = document.choiceForm.move.options.text;
    if (thisitem == selectedText) {
    isNew = false;
    break;
    }
    }
    }
    if (isNew) {
    newoption = new Option(selectedText, selectedValue, false, false);
    document.choiceForm.move.options[boxLength] = newoption;
    }
    document.choiceForm.available.selectedIndex=-1;
    updateList(document.getElementById('move'),
    document.getElementById('newList'));
    }
    shankwheat, Nov 29, 2006
    #1
    1. Advertising

  2. shankwheat

    shankwheat Guest

    Please disregard this question. I worked it out. Thanks.


    shankwheat wrote:
    > I have a form named 'choiceForm' and 2 select boxes named 'available'
    > and 'move'. I give users the ability of copying options (not moving)
    > from available to move with the code below. I would like to create a
    > button which copies all options from available to move but not sure how
    > to do this. Thanks for any help.
    >
    > function moveOver()
    > {
    > var boxLength = document.choiceForm.move.length;
    > var selectedItem = document.choiceForm.available.selectedIndex;
    > var selectedText =
    > document.choiceForm.available.options[selectedItem].text;
    > var selectedValue =
    > document.choiceForm.available.options[selectedItem].value;
    >
    > var i;
    > var isNew = true;
    >
    > if (boxLength != 0) {
    > for (i = 0; i < boxLength; i++) {
    > thisitem = document.choiceForm.move.options.text;
    > if (thisitem == selectedText) {
    > isNew = false;
    > break;
    > }
    > }
    > }
    > if (isNew) {
    > newoption = new Option(selectedText, selectedValue, false, false);
    > document.choiceForm.move.options[boxLength] = newoption;
    > }
    > document.choiceForm.available.selectedIndex=-1;
    > updateList(document.getElementById('move'),
    > document.getElementById('newList'));
    > }
    shankwheat, Nov 29, 2006
    #2
    1. Advertising

  3. shankwheat

    ASM Guest

    shankwheat a écrit :
    > I have a form named 'choiceForm' and 2 select boxes named 'available'
    > and 'move'. I give users the ability of copying options (not moving)
    > from available to move with the code below. I would like to create a
    > button which copies all options from available to move but not sure how
    > to do this. Thanks for any help.


    <script type="text/javascript">

    function selectMove()
    {
    var from = document.choiceForm.available;
    var k = from.selectedIndex
    var selectedText = from.options[k].text;
    var selectedValue = from.options[k].value;

    var to = document.choiceForm.move;

    // to do not copy same option several times
    var OK = true
    for(var i=0; i<to.length; i++)
    if(to.value==selectedValue) OK = false;

    if(OK)
    to[to.length] = new Option(selectedText, selectedValue);
    else
    alert('option already copied');
    }

    </script>

    <form name="choiceForm">

    <select name="available" onchange="selectMove()">
    <option value="a">A
    <option value="b">B
    <option value="c">C
    <option value="d">D
    <option value="e">E
    <option value="f">F
    </select>

    <select name="move"
    onchange="var k=this.selectedIndex;
    alert(this[k].value+' '+this[k].text);">
    </select>
    </form>




    --
    Stephane Moriaux et son moins vieux Mac déjà dépassé
    Stephane Moriaux and his less old Mac already out of date
    ASM, Nov 29, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cliff  Martin
    Replies:
    1
    Views:
    3,022
    Larry Smith
    Jan 31, 2007
  2. jacksu
    Replies:
    0
    Views:
    498
    jacksu
    Oct 9, 2007
  3. Utkado
    Replies:
    2
    Views:
    2,062
  4. Replies:
    4
    Views:
    77
Loading...

Share This Page