Solved

Javascript: user adding items to a predefined dual select box

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Web experts ! HTML ! Javascript project for medical MRI sharing 24 52
How to deal with the query string 8 38
removing a class in javascript 4 51
Clear input text 15 9
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

820 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