dynamically filling a select option
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: dynamically filling a select option

  1. #1
    Join Date
    Mar 2009
    Posts
    3

    Angry 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>
    Last edited by PeejAvery; March 4th, 2009 at 08:26 AM. Reason: Added code tags.

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,868

    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");
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Mar 2009
    Posts
    3

    Re: dynamically filling a select option

    Ok, but that doesn't fix my problem. I just had that in there for debugging purposes.

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,868

    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.
    Last edited by PeejAvery; March 4th, 2009 at 12:12 PM.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Mar 2009
    Posts
    3

    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'

  6. #6
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,868

    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>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center