Solved

Javascript: user adding items to a predefined dual select box

Posted on 2008-06-24
3
385 Views
Last Modified: 2008-07-03
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
Comment
Question by:JonFleming
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
GaryRasmussen earned 50 total points
ID: 21861640
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
 
LVL 1

Expert Comment

by:GaryRasmussen
ID: 21861653
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
 

Author Comment

by:JonFleming
ID: 21864301
Ah, thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now