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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Dynamically Populate Comboboxes with Textarea Values

Status
Not open for further replies.

42DoubleDDs

IS-IT--Management
Jun 23, 2010
2
US

´*•.¸(`*•.¸?¸.•*´)¸.•*´
?«´•°*42DoubleDDs*°•´»?
.¸.•*(¸.•*´?`*•.¸) *•.¸


How can I dynamically populate two comboboxes with the Values of a textarea?

Script below will dynamically populate a single combobox, one entry at a time, from an input box.

Would like to:
-dynamically build textarea
-dynamically build, populate comboboxes
-split between punctuation (;) and sort Comboboxes by name (acs)

Thank You........@}-,-`-

RESULTS WOULD LOOK LIKE THIS:

Textarea:
________________________________
Mr. Smith;JSmith@hotmail.com;
Ms. Jones;TJones@hushmail.com;
Mr. White;AWhite@Yahoomail.com;
________________________________

Combobox1
________________________________
Mr. Smith
Mr. White
Ms. Jones
________________________________

Combobox2
________________________________
JSmith@hotmail.com
AWhite@Yahoomail.com
TJones@hushmail.com
________________________________


Code:
<HTML>
	<HEAD>
		<TITLE>Dynamically populating drop down, combobox, list box using JavaScript</TITLE>
		<SCRIPT language="javascript">
function addCombo() {
	var textb = document.getElementById("txtCombo");
	var combo = document.getElementById("combo");
	
	var option = document.createElement("option");
	option.text = textb.value;
	option.value = textb.value;
	try {
		combo.add(option, null); //Standard 
	}catch(error) {
		combo.add(option); // IE only
	}
	textb.value = "";
}
</SCRIPT>
	</HEAD>
	<BODY style="font-family: sans-serif">

		<fieldset>

			<legend>Combo box</legend>
			Add to Combo: <input type="text" name="txtCombo" id="txtCombo"/>
			<input type="button" value="Add" onclick="addCombo()">
			<br/>
			Combobox: <select name="combo" id="combo"></select>

		</fieldset>
	</BODY>
</HTML>
 
You'll have to use the split function to break up the textarea into lines, then break up the lines into its parts and build the boxes:

Code:
var tcont=document.getElementById('textareaID');
var lines=tcont.split("\n");

and then spitting the lines into their parts. 

var names=lines.split(";")[0];
var emails=lines.split(";")[1];

You will of course have to loop through the lines to get the parts, but this should give you a starting point.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
You are too kind sir!

Thank You so much for the prompt and valuable response.

´*•.¸(`*•.¸?¸.•*´)¸.•*´
?«´•°*42DoubleDDs*°•´»?
.¸.•*(¸.•*´?`*•.¸) *•.¸
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top