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

Dynamic Select box load to variable

Status
Not open for further replies.

timmbo

Programmer
Feb 22, 2001
167
US
Hi All,

Trying to load the options contents from my selection box SelectedAccounts to a variable. I move accounts from AvailAccounts to SelectedAccounts via js code. When I hit submit button, code cycles through each account in the SelectedAccounts select box but does not load variable "output".

HTML Code:
Code:
<SELECT name="AvailAccounts" multiple size="10" style="width:240px;" width="235">
  <option>AA Test Inc. [AAB] [123456789]</option>
  <option>Demo Company [CTX] [987654321]</option>
  <option>BB Text Account [DTD] [333333333]</option>
</SELECT>

<SELECT name="SelectedAccounts" multiple size="10" style="width:240px;" width="235">
</SELECT>

Here is my JavaScript code:
Code:
...
var selAccts = document.forms[frmName].SelectedAccounts.options.length;
	
	if (selAccts == 0) {
		alert("Please choose at least one company ID.");
		document.forms[frmName].AvailAccounts.focus();
		return;
	}else {
		var output = '';		
		for (var i=0, l=document.forms[frmName].SelectedAccounts.options.length;i<l;i++) {
			alert("i is ... " + i);
	  		if (i == 0) {
			 	output += escape(document.forms[frmName].SelectedAccounts.options[i].value);
				alert("output1 is ... " + output);
			}else {
				output += ',' + escape(document.forms[frmName].SelectedAccounts.options[i].value);
				alert("output2 is ... " + output);
			}
	  	}
		document.forms[frmName].selAccounts.value = output;
		output = '';
	}
...

Any help would be appreciated.

TIA,
Tim
 

but does not load variable "output".

Do you mean than "output" is not being set, or that "output" is not being written into "selAccounts", or something entirely different?

Aare you getting any error messages? What browser does it not work in? Do you get any of your alerts?

I can't see much wrong with the code you've given above - so it may be other code on your page. Do you have a URL that you can post?

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]

 
Thanks for the response Dan. Sorry for not being clear on this. output is not being set. I don't url to post yet. I will comment out other code in the function to make sure all is good.
 
More info. I'm not getting any error msgs. working in IE 6 and I do get the alerts.
 

Your options have no values to append t output.

Dan



[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]

 

To elaborate, to add values, use this:

Code:
<option [b]value="some value here"[/b]>text</option>

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]

 
SelectedAccounts option is loaded - dynamically. I tried you suggestion by adding a value to Selected Accounts and got the same result. I'm beginning to think I have something else going on in my code.
 

When you dynamically copy the options over, are you copying the values as well?

So... you'd need to:

1. Ensure your original options have values, and
2. Ensure those values are copied

I cannot comment on the option-copying code you've got, as to date, we've seen nothing of it.

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]

 
Thanks for your help Dan. I got it. I replaced .value with .text and it's working fine now.

Old code:
Code:
if (i == 0) {
  output += escape(document.forms[frmName].SelectedAccounts.options[i].value);
  alert("output1 is ... " + output);
}else {
  output += ',' + escape(document.forms[frmName].SelectedAccounts.options[i].value);
  alert("output2 is ... " + output);
}

New code:
Code:
if (i == 0) {
  output += escape(document.ACHCustomRptForm.SelectedAccounts.options[i].text);
}else {
  output += ',' + escape(document.ACHCustomRptForm.SelectedAccounts.options[i].text);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top