Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript: user adding items to a predefined dual select box

Posted on 2008-06-24
3
Medium Priority
?
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
GaryRasmussen earned 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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 …
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…

688 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