Remove an item from a select box

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

    #1

    Remove an item from a select box


    Hi,
    I've got a small javascript problem and I'm kinda stuck. I'm using
    classic ASP. I have a select box which is populated by a database query
    and there is a buttom that when clicked it will move the selected item
    from the first select box and move it to another select box:

    document.form2. list.options[document.form2. list.options.le ngth] = new
    Option(count + 1 + ". " + name,company);

    Then I want to remove it from the first select box:
    document.form1. companies.optio ns[document.form1. companies.selec tedIndex]
    .text = "";

    But that just leaves an empty spot. I need to remove that spot
    altogether and move all the other items up by one.
    This is what I have:
    for (i=document.for m1.companies.op tions.length-1; i>0;
    i--){document.form 1.companies.opt ions[document.form1. companies.selec tedI
    ndex].value = i;
    }

    But this doesn't work.
    Anyone have any ideas?

    Thanks

    Rob

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • mscir

    #2
    Re: Remove an item from a select box

    Rob wrote:[color=blue]
    > Hi,
    > I've got a small javascript problem and I'm kinda stuck. I'm using
    > classic ASP. I have a select box which is populated by a database query
    > and there is a buttom that when clicked it will move the selected item
    > from the first select box and move it to another select box:[/color]
    <snip>

    Really Nice Code:



    Mike

    Comment

    • Rob

      #3
      Re: Remove an item from a select box

      I know...I'm not big on Javascript.
      Thanks for the code.

      Rob



      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • RobB

        #4
        Re: Remove an item from a select box

        Rob wrote:[color=blue]
        > Hi,
        > I've got a small javascript problem and I'm kinda stuck. I'm using
        > classic ASP. I have a select box which is populated by a database[/color]
        query[color=blue]
        > and there is a buttom that when clicked it will move the selected[/color]
        item[color=blue]
        > from the first select box and move it to another select box:
        >
        > document.form2. list.options[document.form2. list.options.le ngth] = new
        > Option(count + 1 + ". " + name,company);
        >
        > Then I want to remove it from the first select box:
        >[/color]
        document.form1. companies.optio ns[document.form1. companies.selec tedIndex][color=blue]
        > text = "";
        >
        > But that just leaves an empty spot. I need to remove that spot
        > altogether and move all the other items up by one.
        > This is what I have:
        > for (i=document.for m1.companies.op tions.length-1; i>0;
        >[/color]
        i--){document.form 1.companies.opt ions[document.form1. companies.selec tedI[color=blue]
        > ndex].value = i;
        > }
        >
        > But this doesn't work.
        > Anyone have any ideas?
        >
        > Thanks
        >
        > Rob
        >
        > *** Sent via Developersdex http://www.developersdex.com ***
        > Don't just participate in USENET...get rewarded for it![/color]

        <html>
        <head>
        <script type="text/javascript">

        function delSelectedOpt( sel_id)
        {
        var o,
        os,
        len,
        i = 0,
        selobj = document.getEle mentById(sel_id );
        if (selobj && (os = selobj.options) )
        {
        while (o = os[i++])
        if (o.selected)
        {
        for (len = os.length; i < len; ++i)
        os[i - 1] = new Option(os[i].text, os[i].value);
        os[--len] = null;
        }
        }
        }

        </script>
        </head>
        <body>
        <form>
        <select id="sss" size="8">
        <option value="0">Optio n 0</option>
        <option value="1">Optio n 1</option>
        <option value="2">Optio n 2</option>
        <option value="3">Optio n 3</option>
        <option value="4">Optio n 4</option>
        <option value="5">Optio n 5</option>
        <option value="6">Optio n 6</option>
        <option value="7">Optio n 7</option>
        </select>
        <input type="button"
        value="delete selected"
        onclick="delSel ectedOpt('sss') " />
        </form>
        </body>
        </html>

        Comment

        Working...