WelcomeServicesPortfolioKnowledge BaseContact Us
27 Seconds, Inc.


Knowledge base View Article

need hosting?

Need hosting? We use and love our host - CrystalTech.com!
Need hosting? We use and love our host - CrystalTech.com!

Text Disguise - CAPTCHA-image/Human Interactive Proof web services

National Holiday Dates and Bank Holiday Dates - web site that provides the dates of national and bank holidays for the United States, United Kingdom, Ireland and Scotland

Holiday Web Service - web services for programmers to provide the dates of national and bank holidays in their applications for the United States, United Kingdom, Ireland and Scotland

Our Family Heart - web site to help families communicate no matter where they are

What?! You haven't heard about the greatest remote controlled flyer to come out in years?! Checkout the FlyTech DragonFly now!

Move option from 1 list box to another.

Summary: Use this function to move an option from one select box to another.
 
Keywords: LIST
LIST BOX
LISTBOX
MOVE
OPTION
SELECT
Applicable Software: JavaScript
 
Body:

Many times you need to have 2 select boxes and move options from one list to the other. The code below will do just that.

function moveOption(pobjSourceList, pobjTargetList)
{
	var intSourceIndex;
	var intTargetIndex;
		
	if (pobjSourceList.length > 0) {
		if (pobjSourceList.selectedIndex != -1) {
			intSourceIndex = pobjSourceList.selectedIndex;
			intTargetIndex = pobjTargetList.length;
				
			pobjTargetList.length++
			pobjTargetList[intTargetIndex].value = pobjSourceList[intSourceIndex].value;
			pobjTargetList[intTargetIndex].text = pobjSourceList[intSourceIndex].text;
				
			if (intSourceIndex == pobjSourceList.length - 1) {
				pobjSourceList.length--
			} else {
				for (var intCount = intSourceIndex; intCount < pobjSourceList.length - 1; intCount++) {
					pobjSourceList[intCount].value = pobjSourceList[intCount + 1].value;
					pobjSourceList[intCount].text = pobjSourceList[intCount + 1].text;
				}
				pobjSourceList.length--
			}
				
			if (pobjSourceList.length > 0) {
				pobjSourceList.selectedIndex = 0;
			}
				
			pobjTargetList.selectedIndex = pobjTargetList.length - 1;
		} else {
			alert("You must select an item to move.");
		}
	} else {
		alert("There are no items to move.");
	}
}

Here's how you would do it:

<script language="JavaScript">
<!--
	// put function here
// -->
</script>

<form action="#" method="post">
<select name="listA" size="4">
<option value="Apples">Apples</option>
<option value="Oranges">Oranges</option>
<option value="Pears">Pears</option>
</select>

<input type="button" value="Move &gt;" onClick="moveOption(this.form.listA, this.form.listB);">
<input type="button" value="&lt; Move" onClick="moveOption(this.form.listB, this.form.listA);">

<select name="listB" size="4"></select>
</form>

A more complete example can be found at www.clearviewdesign.com/NEWBIE

 
Author: Douglas L. Setzer, II, http://www.27seconds.com
Posted On: 3/5/2002 12:02:22 PM

Rate this article: Average: n/a
n/a12345678910
Comments?

Article Search   |   All Articles

 

 
©2002 27 Seconds, Inc. All Rights Reserved.