Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving items from one list to another 1

Status
Not open for further replies.

gameon

Programmer
Feb 23, 2001
325
0
0
GB
Hi, I have two list boxes. (always showing all of their contents).

I would like to be able to move the content of one to the other (or the other way round) by clicking little arrow buttons.

Has anyone seen this done before?

M@
 
By moving, do you mean something like this:

List1:
Red
Orange
Yellow
Green

List2:
Artichokes
Bratwurst
Crusts
Doubledecker Sandwiches

Then, you select "Green" from the first list and click the ">" button and you end up with:

List1:
Red
Orange
Yellow

List2:
Green
Artichokes
Bratwurst
Crusts
Doubledecker Sandwiches

Like that?

Cheers,


[monkey] Edward [monkey]

"Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
That's exactly what I mean. I've set up a database. I'm just building an interface to it. There is a bank of items, I would like the users to be able to use the method that you have described to move stuff arround...

M@)
 
So, you already have your web page populating the listboxes from tables in the database? If so, then it's a matter of juggling the table contents, then telling your page to refresah the listboxes, right?

Cheers,


[monkey] Edward [monkey]

"Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
Yep, I just don't know how to dynamicall move an item from one list box to another...

(maybe I should have just said that) - i'm quite tired...
 
I must think on this, but part of that thinking will be going over: faq216-335

So, you might want to see if that's helpful to you. I've been trying to bullwhip this same functionality on a project of my own and haven't been terribly successful.

I'll let you know if I figure it out, but if someone else has, I hope they mention it here.

Cheers,


[monkey] Edward [monkey]

"Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
cheers mate, will read through it...
M@)
 
there is a simple little way to do this that is cross browser.

Here is the jist of it. Something to make you guys wonder about what else I will come up with! I'll be coding something better but that can probably give you guys an idea while I code something else! :)

<HTML>
<HEAD>
<SCRIPT>
function moveOption(sourceSelect, targetSelect, index)
{
var index = (index == null) ? sourceSelect.selectedIndex : -1 ;
if (index == -1)
{
alert(&quot;Please select one item to move&quot;)
return
}

var option = sourceSelect.options[index]
var temp = new Option(option.text, option.value)
sourceSelect.options[index] = null
targetSelect.options[targetSelect.options.length] = temp
}

</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=&quot;myForm&quot;>
<select name=&quot;sel1&quot; size=&quot;7&quot;>
<option value=&quot;0&quot;>Beer</option>
<option value=&quot;1&quot;>Vodka</option>
<option value=&quot;2&quot;>Wine</option>
<option value=&quot;3&quot;>Wiskey</option>
</select>
<input type=&quot;button&quot; onclick=&quot;moveOption(document.myForm.sel1, document.myForm.sel2)&quot;>

<select name=&quot;sel2&quot; size=&quot;7&quot;>
</select>



</FORM>
</BODY>
</HTML>

PS: Edward, why didn't you ask if you had problems with a script?

Gary Haran
==========================
 
Ok so here is another similar script that is more flexible. You can have multiple options selected or just a single one! :)

I hope you guys enjoy this! :)

<HTML>
<HEAD>
<SCRIPT>
function moveOption(sourceSelect, targetSelect)
{
var index = new Array()
for (var i = 0; i < sourceSelect.options.length; i++)
{
if (sourceSelect.options.selected)
{
index.push(i)
}
}

if (index.lenght == 0)
{
alert(&quot;Please select at least one item to move&quot;)
return
}

while(index.length != 0)
{
var i = index.pop()
var option = sourceSelect.options
var temp = new Option(option.text, option.value)
sourceSelect.options = null
targetSelect.options[targetSelect.options.length] = temp
}
}

</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=&quot;myForm&quot;>
<select name=&quot;sel1&quot; size=&quot;7&quot; multiple>
<option value=&quot;0&quot;>Beer</option>
<option value=&quot;1&quot;>Vodka</option>
<option value=&quot;2&quot;>Wine</option>
<option value=&quot;3&quot;>Wiskey</option>
<option value=&quot;4&quot;>Sling</option>
<option value=&quot;5&quot;>Amarreto Sour</option>
<option value=&quot;6&quot;>Southern Comfort</option>
<option value=&quot;7&quot;>Amarula</option>
</select>
<input type=&quot;button&quot; onclick=&quot;moveOption(document.myForm.sel1, document.myForm.sel2)&quot; value=&quot;>>&quot;>
<input type=&quot;button&quot; onclick=&quot;moveOption(document.myForm.sel2, document.myForm.sel1)&quot; value=&quot;<<&quot;>

<select name=&quot;sel2&quot; size=&quot;7&quot; multiple>
</select>



</FORM>
</BODY>
</HTML>

PS : Edward, Shadows of Forgotten Ancestors by Carl Sagan and Ann Druyan is awesome! :)


Gary Haran
==========================
 
excellent!!

I tried adding something so that I could swap the vals round by it got angary with me?

-------------------------------------------------------<HTML>
<HEAD>
<SCRIPT>
function moveOption(sourceSelect, targetSelect, index)
{
var index = (index == null) ? sourceSelect.selectedIndex : -1 ;
if (index == -1)
{
alert(&quot;Please select one item to move&quot;)
return
}

var option = sourceSelect.options[index]
var temp = new Option(option.text, option.value)
sourceSelect.options[index] = null
targetSelect.options[targetSelect.options.length] = temp
}

</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=&quot;myForm&quot;>
<select name=&quot;sel1&quot; size=&quot;7&quot;>
<option value=&quot;0&quot;>Beer</option>
<option value=&quot;1&quot;>Vodka</option>
<option value=&quot;2&quot;>Wine</option>
<option value=&quot;3&quot;>Wiskey</option>
</select>
<input type=&quot;button&quot; onclick=&quot;moveOption(document.myForm.sel1, document.myForm.sel2)&quot;>
<input type=&quot;button&quot; onclick=&quot;moveOption(document.myForm.sel2, document.myForm.sell)&quot;>

<select name=&quot;sel2&quot; size=&quot;7&quot;>
</select>



</FORM>
</BODY>
</HTML>
 
it would help if I could read (typo) sel1 not sell.

I need sleep.

Goodnight..

M@)
 
I take it the bigger challenge is multiple (holding down control) selects?

M@
 
Sorry I had forgotten to uncheck Process TGML

<HTML>
<HEAD>
<SCRIPT>
function moveOption(sourceSelect, targetSelect)
{
var index = new Array()
for (var i = 0; i < sourceSelect.options.length; i++)
{
if (sourceSelect.options.selected)
{
index.push(i)
}
}

if (index.lenght == 0)
{
alert(&quot;Please select at least one item to move&quot;)
return
}

while(index.length != 0)
{
var i = index.pop()
var option = sourceSelect.options
var temp = new Option(option.text, option.value)
sourceSelect.options = null
targetSelect.options[targetSelect.options.length] = temp
}
}

</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=&quot;myForm&quot;>
<select name=&quot;sel1&quot; size=&quot;7&quot; multiple>
<option value=&quot;0&quot;>Beer</option>
<option value=&quot;1&quot;>Vodka</option>
<option value=&quot;2&quot;>Wine</option>
<option value=&quot;3&quot;>Wiskey</option>
<option value=&quot;4&quot;>Sling</option>
<option value=&quot;5&quot;>Amarreto Sour</option>
<option value=&quot;6&quot;>Southern Comfort</option>
<option value=&quot;7&quot;>Amarula</option>
</select>
<input type=&quot;button&quot; onclick=&quot;moveOption(document.myForm.sel1, document.myForm.sel2)&quot; value=&quot;>>&quot;>
<input type=&quot;button&quot; onclick=&quot;moveOption(document.myForm.sel2, document.myForm.sel1)&quot; value=&quot;<<&quot;>

<select name=&quot;sel2&quot; size=&quot;7&quot; multiple>
</select>



</FORM>
</BODY>
</HTML>


Gary Haran
==========================
 
I would like to buy you one of each of those...

M@)
 
Hi Again, gary. The form is working nice. But, when I submit it, it does not seam to pass the contents of the values... any advise?

M@)
 
I'm so dumb, it was becuase none of the items are selected....

M@
 
Great script Gary...

Once I've moved some options over to the new select, the same number of options are highlighted in the old (highlighing values that have now moved up into the old positions)... What's the best way to prevent this highlighting?

Thanks,
~Lindsay
 
Great script Gary,

I often find myself challenged with how to do things. Yet I try not to post for an answer ( being relatively new to javascript I find that I can retain the lesson better when I figure out things by working them out ) however, I would not be able to overcome the problems if it was not for the input that you and others do in this forum that gives me a path to follow. That and referring to the books! (*) you deserve it.

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top