Michael Meyers
Michael Meyers

Reputation: 13

get value from selection in dropdown menu with variable name

I have created a HTA page to get the status of workstations filtered from Active Directory. Currently I added buttons to perform specific actions for the workstation that has the button next to is.

Since I'm adding more and more functions, I would like to use a dropdown box. The size of the list with workstations is variable depending on the input made by the user, so I've given the name of the worstation to the dropdown box.

The problem I have now is that I cannot retreive the selected value in my VBScript code. If I put listbox1.value I get the correct value in return. If I use place the name of the listbox in a variable like this, it doesn't work.

strlistbox = "listbox1
listbox1.value

I get an error.

enter image description here

This is part of the code I'm using. In total I have 760 lines, so I won't paste everything here.

VBScript code:

Sub RunOption(strCPU)
  dim lstname
  dim intNumber

  lstname = "list"&strCPU      'Value of lstname = listWPBED3702885

  msgbox listWPBED3702885.value    'If I try this messagebox, I get the value of the selected option form the dropdown list names "listWPBED3702885"

  intNumber = lstname.value 'This doesn't seem to work

  Select Case list 'intNumber
    Case 1   Do something
    Case 2   Do something
    Case 3   Do something
    Case 4   Do something
  End Select
End Sub

HTML code:

strHTML = strHTML & "<td width='1'><select class='select' size='1' title='list" & _
          objRecordSet.Fields("Name") & "' name='list' onChange=""RunOption('" & _
          objRecordSet.Fields("Name") & "')"">"& _
          "<option value='0' selected>Options...</option>" & _
          "<option value='1'>C:\ Drive</option>" & _
          "<option value='2'>Computer Management</option>" & _ 
          "<option value='3'>Event Viewer</option>" & _
          "<option value='4'>MAC Address</option>" & _
          "</select></td>"

Upvotes: 0

Views: 12050

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

You can't set a variable to a string and expect that string to behave like an element of your HTML code. You need to fetch the element referenced by the name or ID. The usual way is to define the element you want to use with a unique ID like this:

<select id='uniquevalue' ...>
  ...
</select>

and then get that element via getElementById():

Set listbox = document.getElementById("uniquevalue")
MsgBox listbox.value

Upvotes: 4

Related Questions