Solved

Javascript: user adding items to a predefined dual select box

Posted on 2008-06-24
3
384 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now