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

reorder items in listbox

Status
Not open for further replies.

leskk

MIS
Dec 23, 2003
3
US
Hi,

How to reorder items in listbox for example


I have 5 items in listbox, i want the move up and move down button to move items up and down in listbox.

if anyone know i to do it, thx
 
something like this?
Code:
<html>
	<head>
		<title>test</title>

		<script type=&quot;text/javascript&quot;>
			function move(el, dir) {
				var max = el.options.length;
				//  the selected option
				var i1 = el.selectedIndex;
				var o1 = el.options[i1];
				
				//  the option to switch places with
				var i2 = (i1 + dir) % max;				
				if (i2 < 0) i2 = max - 1;
				var o2 = el.options[i2];
				
				//  temporarily move o1 to very end
				var tmp = el.options[max] = new Option(o1.text, o1.value);
				
				//  move o2 to o1
				el.options[i1] = new Option(o2.text, o2.value);
				
				//  move temp o1 to o2
				el.options[i2] = new Option(tmp.text, tmp.value);
				
				//  remove temp o1
				el.options.length = max;

				el.selectedIndex = i2;
			}
		</script>
	</head>

	<body>
		<form>
			<select name=&quot;sel&quot; size=&quot;9&quot;>
			<option value=&quot;0&quot;>zero</option>
			<option value=&quot;1&quot;>one</option>
			<option value=&quot;2&quot;>two</option>
			<option value=&quot;3&quot;>three</option>
			<option value=&quot;4&quot;>four</option>
			</select>
			<input type=&quot;button&quot; value=&quot;/\&quot; onclick=&quot;move(this.form.sel, -1);&quot; />
			<input type=&quot;button&quot; value=&quot;\/&quot; onclick=&quot;move(this.form.sel, 1);&quot; />
		</form>
	</body>
</html>

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top