PDA

Click to See Complete Forum and Search --> : Js - adding to a select listbox


alex_read
Sep 5th, 2001, 10:52 AM
last huge problem then this project's finished thank god ! :rolleyes:

I have 2 <select> tags acting like vb listboxes on a web page. When a button's clicked, an entry (<option> tag) is taken out of the left select & put into the right select box.

The problem is if you hold shift & multi - select entries. If these multiple entries are next to one another, they don't all copy over. If they aren't next to each other, then they all do copy over & I'm stumped becaus of this. :eek:

Hopefully this code (not too big) should help & I've written some comments in to expain this better.

Thanks All ! ;)

CiberTHuG
Sep 5th, 2001, 11:10 AM
They don't get added if they are adjacent because...

Lets say you have five items, A, B, C, D, and E. A is the first item, so it has an index of 0. Let's say you have selected B, C and E.

It cycles through starting with index 0.

A is not selected.
Next index. Index 1.
B is selected.
Add B to the right.
Remove B from the Left
Next Index. Index 2. Keep in mind that the left is now A, C, D, and E. So index 2 is the third item, that is now D, not C.
D is not selected.
Next index. Index 3.
E is selected.
Add E to the right.
Remove E from the Left
Next Index. Index 4. Index 4 exceeds the length, return from loop.

If you want to immediately remove the item from the left, then subtract 1 from your index (index--), so when it adds one again, it will pick up the next one in the sequence (which has been shortened).

alex_read
Sep 5th, 2001, 11:25 AM
You've been doing this a little while haven't you ;D

YABADABADOO !!!!
Thanks for that (think I'll keep that source handy for ajother time) - makes sense now you explained it, but I'd have never spotted that in a million years. Thanks again ;)