Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add names selected in a combo box to a text box 3

Status
Not open for further replies.

Boblevien

Technical User
Nov 3, 2000
38
GB
Can anyone advise how I can display a list of names in a combo box and add (multiple) selected names to a textbox, delimited by a ';' (semi-colon)? I am trying to build a list of email addresses to pass to a CDONTS mail object.

Cheers,

Bob Levien.
 
Try this:

1. Put the following code in the <HEAD> tags in your page:

<script language=&quot;JavaScript&quot;>
function append_name(){
var bContinue
var NewName
var SelectedNames
var AlreadySelected
bContinue=true
if (bContinue==true){
if (document.frmTest.cboList.value==&quot;NoSelection&quot;){
bContinue=false
}
}
if (bContinue==true){
NewName=document.frmTest.cboList.value;
SelectedNames=document.frmTest.txtNames.value;
AlreadySelected = SelectedNames.indexOf(NewName);
if (AlreadySelected>-1){
bContinue=false
}
}
if (bContinue==true){
if (document.frmTest.txtNames.value!=&quot;&quot;){
document.frmTest.txtNames.value=document.frmTest.txtNames.value+&quot;;&quot;+document.frmTest.cboList.value
} else {
document.frmTest.txtNames.value=document.frmTest.cboList.value
}
}
}
</script>


2. put the following code inside the <SELECT> tag:

onchange=&quot;append_name(); return true&quot;

so that the <SELECT> tag will look something like this:

<select name=&quot;cboList&quot; onchange=&quot;append_name(); return true&quot;>

3. Make the first entry in the list a dummy entry, which does not have an e mail address, but has the value NoSelection
IE make the first <Option> tag something like this:

<option value=&quot;NoSelection&quot;>Select Recipients

NOTE: It is very important that you have the first option in the select list, as then the first e mail address is not selected by default and the change event of the list box will be fired when selecting the first e mail address.
Code within the JavaScript function deals with the user selecting the non-valid e mail address entry


As an example of a complete page, you could have something like this:

<HTML>
<HEAD>
<script language=&quot;JavaScript&quot;>
function append_name(){
var bContinue
var NewName
var SelectedNames
var AlreadySelected
bContinue=true
if (bContinue==true){
if (document.frmTest.cboList.value==&quot;NoSelection&quot;){
bContinue=false
}
}
if (bContinue==true){
NewName=document.frmTest.cboList.value;
SelectedNames=document.frmTest.txtNames.value;
AlreadySelected = SelectedNames.indexOf(NewName);
if (AlreadySelected>-1){
bContinue=false
}
}
if (bContinue==true){
if (document.frmTest.txtNames.value!=&quot;&quot;){
document.frmTest.txtNames.value=document.frmTest.txtNames.value+&quot;;&quot;+document.frmTest.cboList.value
} else { document.frmTest.txtNames.value=document.frmTest.cboList.value
}
}
}
</script>
</HEAD>
<BODY>
<form name=&quot;frmTest&quot; action=&quot;test2.asp&quot; method=&quot;POST&quot;>
<select name=&quot;cboList&quot; onchange=&quot;append_name(); return true&quot;>
<option value=&quot;NoSelection&quot;>Select Recipients
<option value=&quot;email1&quot;>name1
<option value=&quot;email2&quot;>name2
<option value=&quot;email3&quot;>name3
<option value=&quot;email4&quot;>name4
<option value=&quot;email5&quot;>name5
<option value=&quot;email6&quot;>name6
<option value=&quot;email7&quot;>name7
<option value=&quot;email8&quot;>name8
<option value=&quot;email9&quot;>name9
</select><BR><BR><BR>
<input type=&quot;text&quot; name=&quot;txtNames&quot; size=&quot;80&quot;>
</form>
</BODY>
</HTML>


Simon
 
Hi Simon,

Just want to say that your help was right on target - exactly what I was trying to achieve. I didn't expect anything half so detailed - thanks very much.

Bob Levien
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top