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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add/Remove Script between two list boxes

Status
Not open for further replies.

Streetdaddy

Programmer
Jun 10, 1999
161
AU
I haven't done much Java scripting, so I thought I might save some time by asking everyone first!

I have two list-boxes (list1, list2) side by side with two buttons in between; 'add >>' and 'remove <<'

selecting an item from list1 and clicking 'add >>' will add the item to list2. And vice-versa for 'remove <<'.

Does anyone have some code I can steal to save me spending all day trying to work it out myself?

Any help is much appreciated!

Miles. Miles Tillinger
SE Net Web Design
vmiles@senet.com.au
 
Although there's a lot of scripts out there that will do this for you, I'll give you one here...

<script language=&quot;JavaScript&quot;><!--
function deleteOption(object,index) {
object.options[index] = null;
}

function addOption(object,text,value) {
var defaultSelected = true;
var selected = true;
var optionName = new Option(text, value, defaultSelected, selected)
object.options[object.length] = optionName;
}

function copySelected(fromObject,toObject) {
for (var i=0, l=fromObject.options.length;i<l;i++) {
if (fromObject.options.selected)
addOption(toObject,fromObject.options.text,fromObject.options.value);
}
for (var i=fromObject.options.length-1;i>-1;i--) {
if (fromObject.options.selected)
deleteOption(fromObject,i);
}
}

</script>

<form>
<select name=&quot;select1&quot; size=&quot;8&quot;>
<option>1
<option>2
<option>3
<option>4
</select>
<input type=&quot;button&quot; value=&quot;<&quot; onClick=&quot;if (document.images) copySelected(this.form.select2,this.form.select1)&quot;>
<input type=&quot;button&quot; value=&quot;>&quot; onClick=&quot;if (document.images) copySelected(this.form.select1,this.form.select2)&quot;>
<select name=&quot;select2&quot; size=&quot;8&quot;>
<option>5
<option>6
<option>7
<option>8
</select>
</form>

Have fun with it!

<webguru>iqof188</webguru>
 
the reason igof188's code doesn't work is probably because tgml was processed, and a few important characters are missing. adam@aauser.com
 
Any idea which particular characters?

I put in a few alert's trying to debug it. Now it just deletes all the values... Miles Tillinger
SE Net Web Design
vmiles@senet.com.au
 
Here it comes again, without TGML :) :

<script language=&quot;JavaScript&quot;><!--
function deleteOption(object,index) {
object.options[index] = null;
}

function addOption(object,text,value) {
var defaultSelected = true;
var selected = true;
var optionName = new Option(text, value, defaultSelected, selected)
object.options[object.length] = optionName;
}

function copySelected(fromObject,toObject) {
for (var i=0, l=fromObject.options.length;i<l;i++) {
if (fromObject.options.selected)
addOption(toObject,fromObject.options.text,fromObject.options.value);
}
for (var i=fromObject.options.length-1;i>-1;i--) {
if (fromObject.options.selected)
deleteOption(fromObject,i);
}
}

</script>

<form>
<select name=&quot;select1&quot; size=&quot;8&quot;>
<option>1
<option>2
<option>3
<option>4
</select>
<input type=&quot;button&quot; value=&quot;<&quot; onClick=&quot;if (document.images) copySelected(this.form.select2,this.form.select1)&quot;>
<input type=&quot;button&quot; value=&quot;>&quot; onClick=&quot;if (document.images) copySelected(this.form.select1,this.form.select2)&quot;>
<select name=&quot;select2&quot; size=&quot;8&quot;>
<option>5
<option>6
<option>7
<option>8
</select>

Thanks...

<webguru>iqof188</webguru>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top