dynamically filling a select option
I am trying to create a form that will dynamically fill a select list from information retrieved as a recordset from a sql database. I seem to retrieve the information fine. The problem is loading it into the select statement. I am using a vbscript to get the recordset and loop through it and than passing the datavalue that I want listed in the select list to a javascript to add it the select. Where I am having problems with is when I pass the variable to the Javascript it tells me type mismatch on loadselect. Here is the pertinent code. Any help would be greatly appreciated.
Code:
<script type="text/javascript">
Function LoadSelect(team)
{
msgbox "in LoadSelect";
oSelect = document.getElementById("Ugroups");
objNewOption = document.createElement("OPTION");
objNewOption.Value = team.value;
objNewOption.Text = team.value;
oSelect.options.Add(objNewOption);
}
</script>
<script type="text/vbscript">
Function Load(document)
Dim oConn, oRS, strConn
Set oRS = CreateObject("ADODB.Recordset")
Set oConn = CreateObject("ADODB.Connection")
strConn = "Driver={SQL Native .....
oConn.Open strConn
strSQL = "select ......
set oRS = oConn.Execute(strSQL)
Do while not oRS.eof
dim grpstr
grpstr = ""
msgbox Trim(oRS.Fields("gname"))
grpstr = Trim(oRS.Fields("gname"))
LoadSelect grpstr
oRS.movenext
loop
oConn.Close
set oConn = nothing
end Function
</script>
<select id="Ugroups" size="1" style="background-color:#ffff99" name="Ugroups" >
</select>
Re: dynamically filling a select option
JavaScript does not support the following...
Code:
msgbox "in LoadSelect";
In JavaScript, you must use alert().
Code:
alert("in LoadSelect");
Re: dynamically filling a select option
Ok, but that doesn't fix my problem. I just had that in there for debugging purposes.
Re: dynamically filling a select option
You need to make sure that the parameter you are passing to the JavaScript function is truly an object. If you are just passing a string, then you just use team, and not team.value.
Also, I would suggest using the following instead.
EDIT: See code below.
Re: dynamically filling a select option
The parameter I am passing is a string and I think that is my problem. As soon as it hits the line
LoadSelect grpstr
I get a "Type mismatch: 'Load Select'
Re: dynamically filling a select option
There is no value property of a string. So, what you need is...
Code:
<script type="text/javascript">
function LoadSelect(team) {
oSelect = document.getElementById("Ugroups");
oSelect.options[oSelect.options.length] = new Option(team, team);
}
</script>