Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web
Expertise: Beginner
Mar 12, 2002

Swap List Items in a Given Direction

 
<script language="javascript">
//Author : Sudhakar SVS
//Function to Swap the List Items in given direction
//Date   : 11-Apr-2001
//Parameters :	lObj - ListBox Object
//direction - direction to move should be either 'up' _
or 'down'


function swapListItems(lObj,direction)
	{
	if (direction == 'up')
		{
			var temp;
			if (!(lObj.selectedIndex==0))
					{
					_
temp = lObj.options[lObj.selectedIndex-1].text;
					_
lObj.options[lObj.selectedIndex-1].text =
lObj.options[lObj.selectedIndex].text;
					_
lObj.options[lObj.selectedIndex].text = temp;
					}
				}
				else
				{
					var temp;
					if (!(lObj.selectedIndex==lObj.options.length-1))
					{
					temp = lObj.options[lObj.selectedIndex+1].text;
					_
lObj.options[lObj.selectedIndex+1].text =
lObj.options[lObj.selectedIndex].text;
					_
lObj.options[lObj.selectedIndex].text = temp;
					}
				}
			}

			//Function to Move List Items to Up
			function moveUp()
			{

				var listObject = _
eval(document.myForm.mylist);
				if _
(!(listObject.selectedIndex==-1))
				{
				var selected = _
listObject.selectedIndex;
				_
swapListItems(listObject,'up');
				listObject.selectedIndex _
= selected - 1;
				}
				else
				{
					_
alert('Please select an item to move up');
				}
			}


			//Function to Move List _
Items to Down
			function moveDown()
			{
				var listObject = _
eval(document.myForm.mylist);
				if _
(!(listObject.selectedIndex==-1))
				{
				var selected = _
listObject.selectedIndex;
				_
swapListItems(listObject,'down');
				listObject.selectedIndex _
= selected + 1;
				}
				else
				{
					_
alert('Please select an item to move down');
				}

			}
		</script>


//Implementation of JS Functions
//------------------------------

//To Move up
<img name="moveup" id="moveup" src="moveup.gif"
onclick="Javascript:moveUp()" border="0">

//To Move down
<img name="movedown" id="movedown" src="movedown.gif"
onclick="Javascript:moveDown()" border="0">
Sudhakar Sadasivuni
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date