Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Help with moving list box items ASAP


Message #1 by "Chris Cote" <cotec@s...> on Mon, 14 Jan 2002 19:39:55
There's a more direct way of doing this...

Given a listbox with elements named mylistbox and two buttons named btnUp 
and btnDown:

function btnUp_onclick() {
  var StrStoreValue;
  if ((mylistbox.selectedIndex == -1) || (mylistbox.selectedIndex == 0)) 
return;
  strStoreValue = mylistbox.options[mylistbox.selectedIndex - 1].text;
  mylistbox.options[mylistbox.selectedIndex - 1].text = mylistbox.options
[mylistbox.selectedIndex].text;
  mylistbox.options[mylistbox.selectedIndex].text = strStoreValue;
  mylistbox.selectedIndex = mylistbox.selectedIndex - 1;
}

function btnDown_onclick() {
  var strStoreValue;
  if ((mylistbox.selectedIndex == -1) || (mylistbox.selectedIndex == 
mylistbox.options.length - 1)) return;
  strStoreValue = mylistbox.options[mylistbox.selectedIndex + 1].text;
  mylistbox.options[mylistbox.selectedIndex + 1].text = mylistbox.options
[mylistbox.selectedIndex].text;
  mylistbox.options[mylistbox.selectedIndex].text = strStoreValue;
  mylistbox.selectedIndex = mylistbox.selectedIndex + 1;
}

Just call these functions on the onclick events of the Up and Down buttons 
and you're all set =)

  Return to Index