SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Newcastle
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question select array problem?

    Hi,

    I'm having some trouble with select boxes. The boxes I have are below:

    <select name="sel1" size="10" multiple>
    <option>some options</option>
    <?
    }
    ?>
    </select>
    </td>
    <td align="center" valign="middle">
    <input type="button" value="-->" onClick="moveOptions(this.form.sel1, this.form.sel2[]);" /><br />
    <input type="button" value="<--" onClick="moveOptions(this.form.sel2[], this.form.sel1);" />
    </td>
    <td>
    <!--documents assigned to news article-->
    <select name="sel2[]" MULTIPLE size="10">
    <option>some options</option>
    </select>
    </td>
    <td><INPUT TYPE=SUBMIT VALUE="Submit"></td>

    Basically I'm copying data from one to the other

    the code is erroring at moveOptions. I think it is because sub2 is an array which I need it to be.
    The javascript is:

    function moveOptions(theSelFrom, theSelTo)
    {

    var selLength = theSelFrom.length;
    var selectedText = new Array();
    var selectedValues = new Array();
    var selectedCount = 0;

    var i;

    // Find the selected Options in reverse order
    // and delete them from the 'from' Select.
    for(i=selLength-1; i>=0; i--)
    {
    if(theSelFrom.options[i].selected)
    {
    selectedText[selectedCount] = theSelFrom.options[i].text;
    selectedValues[selectedCount] = theSelFrom.options[i].value;
    deleteOption(theSelFrom, i);
    selectedCount++;
    }
    }

    // Add the selected text/values in reverse order.
    // This will add the Options to the 'to' Select
    // in the same order as they were in the 'from' Select.
    for(i=selectedCount-1; i>=0; i--)
    {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
    }

    if(NS4) history.go(0);
    }


    Please, can anyone help?

    Thanks

    Craig

  2. #2
    SitePoint Zealot Valera Selev's Avatar
    Join Date
    Apr 2004
    Location
    Lviv, Ukraine
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will work in browsers which support DOM:

    HTML Code:
    <body>
    <select id="sel1" multiple="multiple">
      <option value="option1">option1</option>
      <option value="option2">option2</option>
    </select>
    <select id="sel2" multiple="multiple">
      <option value="option3">option3</option>
      <option value="option4">option4</option>
    </select>
    <br />
    <script>
    var select1 = document.getElementById('sel1');
    var select2 = document.getElementById('sel2');
    function move(o1,o2) {
      optionsToMove = new Array();
      for (i=0;i<o1.length;i++)
        if (o1.options[i].selected)
    	   optionsToMove[optionsToMove.length]=o1.options[i];
      for (i=0;i<optionsToMove.length;i++)
        o2.appendChild(optionsToMove[i]);
    }
    </script>
    <button onclick="move(select1,select2)">--&gt;</button>
    <button onclick="move(select2,select1)">&lt;--</button>
    </body>
    Very simple, ah?

  3. #3
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Newcastle
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its not really that simply.

    I realise that works but the important point is that I have to pass it as an array, thus sel2[] is required, not just sel2.

    I'm getting the sytax error because of the array I think but I don't know how to fix it.

  4. #4
    SitePoint Zealot Valera Selev's Avatar
    Join Date
    Apr 2004
    Location
    Lviv, Ukraine
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tell me why doesn't my code suit your needs.
    What do you want to "pass as an array"?
    Please explain your problem in details since my code does exactly what you wanted - moving selected options from one <select> to another.

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably for PHP...

    Code:
    <input type="button" value="-->" onClick="moveOptions(this.form['sel1'], this.form['sel2[]']);" /><br />
    http://jibbering.com/faq/faq_notes/s...kets.html#illc
    ::: certified wild guess :::


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •