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 IamaSherpa 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 options to select box on the fly 1

Status
Not open for further replies.

countryboy

Programmer
Apr 29, 2001
6
CA
The following JScript adds an array of options to a select box (provided the select box is not in a form):
<code>for (u=0; u<Count+1; u++)
if (u==0) {
var oOption=document.createElement(&quot;OPTION&quot;);
oSelect.options.add(oOption);
oOption.innerText = (tempArray);
oOption.value = (tempArray);}</code>
Is it possible to dynamically add an array of options to a form select box using VBScript?
 
I do it IN a form with Javascript like this:


<form name=frm1>
<select name=fName onfocus=&quot;fillselects();&quot;></select>
</form>

<script language=JavaScript>
function fillselects(){
for (var i = 0; i < 10; i++){
document.frm1.fName.options[document.frm1.fName.options.length]=
new Option(&quot;display string &quot; + i, &quot;value &quot; + i);
}
}
</script>



all my VBScript attempt failed, and this worked so why looking further?
br
Gerard
 
Gerard, thanx for your reply!
After some tweaking, I found the following code to work.
First, I remove the one (and only) option:

<code>List1.options.remove(0)</code>

Then I loop through my array:

<code>Dim Record(x, 2)
for Count = 0 to x - 1 Set y = Document.CreateElement(&quot;OPTION&quot;)
List1.options.add(y)
y.innerText = Record(Count, 0)
y.value = Count
next</code>

With the onChange event, the List1.value becomes my array reference, as in:

<code>Text0.value = Record(List1.value, 0)
Text1.value = Record(List1.value, 1)
Text2.value = Record(List1.value, 2)</code>

A nifty way of auto-completing form text fields from a list box selection, with the only drawback being that the list box cannot be between the <form></form> tags.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top