CoderZone.org

Category: >> JavaScript >> Add/Remove from Select list Bookmark and Share

<< lastnext >>

Snippet Name: Add/Remove from Select list

Description: Easily add or remove elements form lists.

Comment: (none)

Author: CoderZone
Language: JAVASCRIPT
Highlight Mode: JAVASCRIPT
Last Modified: February 27th, 2009

<script type="text/javascript">
function swap(index, from, to)
{
     to = document.getElementById(to);
     to.options[to.length] = new Option(from.options[index].text, from.options[index].value);
     from.remove(index);
}
</script>
<form>
     <table border="0" style="border-collapse: collapse" width="100%">
          <tr>
               <td valign="top">
               <select size="5" id="unselected" name="unselected" onchange="swap(this.selectedIndex, this, 'selected');">
               <option value="1 - one">1 - one</option>
               <option value="2 - two">2 - two</option>
               <option value="3 - three">3 - three</option>
               </select> </td>
               <td valign="top">
               <select size="5" id="selected" name="selected" onchange="swap(this.selectedIndex, this, 'unselected');">
               </select> </td>
          </tr>
     </table>
</form>
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 6