Javascript: adding/removing values from txtbox from listbox

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Javascript: adding/removing values from txtbox from listbox

Post by cjkeane »

hi everyone,

i'm new to javascript. i have the following code appending emails together together into a textbox when items are selected from a listbox. i have two issues.
1. the first selection passed to the emailcc textbox always appears as:
, johnsmith@home.ca
2. i'd like the ability to click on the listbox again to remove the email addresses selected from the textbox if email addresses in the list box are clicked a second time.

whats the best way to accomplish this? thanks.

<script type="text/javascript" language="JavaScript">
function Copy_listbox(){
document.emailaddresses.emailcc.value += ", " + document.emailaddresses.SelectEMailccListBox.value;
}
</script>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Javascript: adding/removing values from txtbox from list

Post by McInfo »

If the goal is to submit multiple email addresses in a form, try

Code: Select all

<select multiple name="emails[]">
With the multiple attribute, the user is able to select more than one option by holding the Shift or Ctrl key while clicking. The [] in the name attribute means that all values submitted with the name emails[] (all selected options) should be grouped together in an array named emails.

Also, see this example in the manual.
Post Reply