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

Saving Select contents to a var 2

Status
Not open for further replies.

LyndonOHRC

Programmer
Joined
Sep 8, 2005
Messages
603
Location
US
I want to save the contents of a dynamic dropdown to a variable so I can pass it to the action page of a form. I think I think I'm close but still missing something because the field "TheHiddenField" is empty when I get to the action page.

Thanks
Lyndon

Form:
Code:
<form action="report.cfm" method="post" name="EmployeeReportForm">
	<select name="list1" multiple size="10" onDblClick="LoadHiddenField()">
		<option value="Matt">Matt</option>
		<option value="Bob">Bob</option>
		<option value="Bill">Bill</option>
	</select>
	<select name="list2" multiple size="10" onDblClick="LoadHiddenField()">
		<option value="Mary">Mary</option>
		<option value="Jane">Jane</option>
		<option value="Sue">Sue</option>
	</select>
	<input type="Hidden" value="" name="TheHiddenField">
	<input type="Submit" value="Print Preview">
</form>

Javascript:
Code:
function LoadHiddenField() {
	var ListContents = "";
	for ( var idx = 0; idx < document.EmployeeReportForm.list2.length; idx++ ) {
		ListContents+=document.EmployeeReportForm.list2.options[idx].value&",";
		}
	document.EmployeeReportForm.TheHiddenField=ListContents;
}

 
Now the value of form.TheHiddenField in the action page is 0.

Getting closer, it should be "Mary,Jane,Sue" if I ever get it right.
 
[tt]function LoadHiddenField() {
var ListContents = "";
var oelem=document.EmployeeReportForm.list2;
for ( var idx = 0; idx < oelem.length; idx++ ) {
ListContents += oelem.options[idx].value;
ListContents += (idx==oelem.length-1)?",":"";
}
document.EmployeeReportForm.TheHiddenField.value = ListContents;
}
[/tt]
 
Amendment
I got the line in reverse.
[tt] ListContents += (idx==oelem.length-1)?"":",";[/tt]

ps: the main problem besides .value is the use of "&" which should be "+".
 
Thank you very much. It works perfectly.

Lyndon
 
tsuji,

I'm really new at this. If you have a moment would you explain the use of the ? and the : in this statement?

It looks like a if then else but I'm not clear on the use of the operators.
Code:
ListContents += (idx==oelem.length-1)?",":"";
Thanks
Lyndon
 
Lyndon, that is known as the ternary operator and is often used as a shorthand to an if/then/else condition. They are super handy and I use them all the time

Here's a description:

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Thanks kaht,

Great reference site.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top