Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

Javascript: user adding items to a predefined dual select box

I'm setting up a form for users to submit our papers/presentations to a database. One item is a list of authors. all papers will be authored by at least one and perhaps several of a predefined list of names (our employees). But some of them will include someone from outside our organizatIon, and I need to accommodate that possibility. So the user has to be able to add an arbitrary item to the list of authors. And ther ehave to be "Move up" and "Move down" buttons because the user isn't necessarily going to add then names in the correct order.

I've found some javascript that can do almost all that I want except for adding a name, at http://www.applicationgroup.com/tutorials/DualListBox/DualListBox_js.asp. I'm no javascript expert, but I can hack some code to support an "Add a Name" button. But I thought I'd ask to see if someone has such code lying around in the pile ...
0
JonFleming
Asked:
JonFleming
  • 2
1 Solution
 
GaryRasmussenCommented:
here ya go ...

<html>
<head>
<script language="javascript">
function SwapItem(s, itemPos, swapPos)
{      var tempOption = new Array(s.options[swapPos].text, s.options[swapPos].value);
    s.options[swapPos].text = s.options[itemPos].text;
    s.options[swapPos].value = s.options[itemPos].value;
    s.options[itemPos].text = tempOption[0];
    s.options[itemPos].value = tempOption[1];
    s.selectedIndex = swapPos;
}

function MoveUp()
{      var list = document.getElementById("cboAuthors")
      if (list.selectedIndex != -1 && list.selectedIndex != 0)
      {      SwapItem(list, list.selectedIndex, list.selectedIndex - 1)
      }
}

function MoveDown()
{      var list = document.getElementById("cboAuthors")
      if (list.selectedIndex != -1 && list.selectedIndex != (list.length -1))
      {      SwapItem(list, list.selectedIndex, list.selectedIndex + 1)
      }
}

function Add()
{      var list = document.getElementById("cboAuthors")
      var newAuthor = document.getElementById("txtNewAuthor").value
      
      var option = new Option(newAuthor, newAuthor)
    list.options[list.options.length] = option
}

function Remove()
{      var list = document.getElementById("cboAuthors")
      if (list.selectedIndex != -1)
      {      list.options[list.selectedIndex] = null
      }
}

</script>
</head>
<body>
<form>
<select id="cboAuthors" size="10" name="cboAuthors">
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Horse">Horse</option>
<option value="Fish">Fish</option>
</select>

<br><br>

<input type="button" value="Move Up" onclick="MoveUp()">
<input type="button" value="Move Down" onclick="MoveDown()">
<input type="button" value="Remove" onclick="Remove()">

<br><br>

New Author:<input type="text" id="txtNewAuthor"><input type="button" value="Add" onclick="Add()">

</form>
</body>
</html>
0
 
GaryRasmussenCommented:
Updated the Add function to make sure there is actually something to add

function Add()
{      var list = document.getElementById("cboAuthors")
      var newAuthor = document.getElementById("txtNewAuthor").value
      
      if (newAuthor != "")
      {      var option = new Option(newAuthor, newAuthor)
            list.options[list.options.length] = option
      }
}
0
 
JonFlemingAuthor Commented:
Ah, thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now