populating eslctboxes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bigbuddha

    populating eslctboxes

    Hello,

    I have a form two multiple selectboxes.
    Firs one with options, second one empty
    I need to select a few items and hit an "add" button to get them in
    the second one. Preferably a "remove" button to.
    the second selectbox's name is "gen_list[]' cause i need it in for db
    handling. This might be something to keep in mind.

    If it's possible a re-useable funtion so i can use it for different
    slectboxes.

    someone ??
    thank you.

  • Lasse Reichstein Nielsen

    #2
    Re: populating eslctboxes

    bigbuddha <newsmail@bigbu ddha.be> writes:
    [color=blue]
    > Hello,
    >
    > I have a form two multiple selectboxes.
    > Firs one with options, second one empty
    > I need to select a few items and hit an "add" button to get them in
    > the second one. Preferably a "remove" button to.
    > the second selectbox's name is "gen_list[]' cause i need it in for db
    > handling. This might be something to keep in mind.
    >
    > If it's possible a re-useable funtion so i can use it for different
    > slectboxes.[/color]

    It should be possible to make one.
    ---
    <script type="text/javascript">
    function moveOption(from ,index,to) {
    var origOpt = from.options[index];
    var newOpt = new Option(origOpt. text,origOpt.va lue);
    from.options[index]=null;
    to.options[to.options.leng th] = newOpt;
    }

    function moveSelectedOpt ions(from,to) {
    var i=0;
    while (i<from.options .length) {
    if (from.options[i].selected) {
    from.options[i].selected=false ;
    moveOption(from ,i,to);
    } else {
    i++;
    }
    }
    }
    </script>
    ---
    And an example use of it:
    ---
    <form action="" onsubmit="retur n false">
    <select multiple="multi ple" name="sel1"
    ondblclick="mov eOption(this,th is.selectedInde x,this.form.ele ments.sel2)">
    <option value="X1">Text 1</option>
    <option value="X2">Text 2</option>
    <option value="X3">Text 3</option>
    <option value="X4">Text 4</option>
    <option value="X5">Text 5</option>
    </select>
    <input type="button" value="add"
    onclick="moveSe lectedOptions(t his.form.elemen ts.sel1,
    this.form.eleme nts.sel2);">
    <input type="button" value="remove"
    onclick="moveSe lectedOptions(t his.form.elemen ts.sel2,
    this.form.eleme nts.sel1);">
    <select multiple="multi ple" name="sel2"
    ondblclick="mov eOption(this,th is.selectedInde x,this.form.ele ments.sel1)">
    </select>
    </form>
    ---
    Tested in IE 6, Opera 7, Mozilla FB, and Netscape 4, where only Netscape 4
    looked bad and didn't understand ondblclick.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • bigbuddha

      #3
      Re: populating eslctboxes

      On Sun, 04 Jan 2004 18:33:27 +0100, Lasse Reichstein Nielsen
      <lrn@hotpop.com > wrote:
      [color=blue]
      >bigbuddha <newsmail@bigbu ddha.be> writes:
      >[color=green]
      >> Hello,
      >>
      >> I have a form two multiple selectboxes.
      >> Firs one with options, second one empty
      >> I need to select a few items and hit an "add" button to get them in[/color][/color]
      <script></script>[color=blue]
      >/L[/color]

      Thanks that worked great, i only can give the second selectbox the
      name "list[]". The square brackets don't seem to work in the
      javascript. Any sollution for this?

      thansk!!

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: populating eslctboxes

        bigbuddha <newsmail@bigbu ddha.be> writes:
        [color=blue]
        > Thanks that worked great, i only can give the second selectbox the
        > name "list[]". The square brackets don't seem to work in the
        > javascript. Any sollution for this?[/color]

        That's a Frequently Asked Question. Check
        <URL:http://jibbering.com/faq/#FAQ4_39>
        and
        <URL:http://jibbering.com/faq/#FAQ4_25>
        (although we have since found out that *names* lie "list[]" are not
        illegal in (X)HTML, the solution still works).

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Bigbuddha

          #5
          Re: populating eslctboxes

          > That's a Frequently Asked Question. Check[color=blue]
          > <URL:http://jibbering.com/faq/#FAQ4_39>
          > and
          > <URL:http://jibbering.com/faq/#FAQ4_25>
          > (although we have since found out that *names* lie "list[]" are not
          > illegal in (X)HTML, the solution still works).
          >
          > /L[/color]

          thnk you lasse for setting me up. I have looked at both links but i cant
          seem to implement it.

          while (i< ["myselect[]"].options.length ) {
          if ( ["myselect[]"].options[i].selected) {
          ["myselect[]"].options[i].selected=false ;
          moveOption( ["myselect[]"],i,to);
          } else {
          i++;
          }
          }

          does not work.
          How do i implement the ["myselect[]"].options.length correctly?

          thanks again

          --
          Bigbuddha


          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: populating eslctboxes

            Bigbuddha wrote:

            Who wrote this? Please include a *short* attribution, like the above.
            vvvvvvvv[color=blue][color=green]
            >> [FAQ][/color]
            >
            > thnk you lasse for setting me up. I have looked at both links but i cant
            > seem to implement it.
            >
            > while (i< ["myselect[]"].options.length ) {
            > if ( ["myselect[]"].options[i].selected) {
            > ["myselect[]"].options[i].selected=false ;
            > moveOption( ["myselect[]"],i,to);
            > } else {
            > i++;
            > }
            > }[/color]

            This is fantasy code. "[...]" initializes an Array object which does
            neither have an native "options" property nor has this non-existing
            object a "length" property. Read the FAQ, specs and manuals, and use
            the "elements" collection instead. Additionally, you /can/ use the
            "while" statement, but ECMAScript provides also a C-like "for" statement
            which is good use for iteration. This also avoids the next error: You
            must increment *always*, else you will not iterate at all.
            [color=blue]
            > does not work.[/color]

            "Does not work" is a useless error description.

            Read <3FDBC2B2.10209 08@PointedEars. de> for details.
            [color=blue]
            > How do i implement the ["myselect[]"].options.length correctly?[/color]

            Quickhack:

            // Insert the form element's index (zero-based), id or name here
            // ,-----------------------------'
            // v
            var oSelect = document.forms[...].elements['myselect[]'];
            if (oSelect)
            {
            for (var i = 0; i < oSelect.options .length; i++)
            {
            var o = oSelect.options[i];
            if (o.selected)
            {
            o.selected = false;
            moveOption(oSel ect, i, to);
            }
            }
            }


            HTH

            PointedEars

            P.S.
            You should care more about your Subject header. It is the first thing
            one sees of your posting. If the Subject is already bad, what should
            one expect from the article?

            Comment

            Working...