Disable list if option selected - vbscript

My code is partially working. The list gets disabled but it's not catching that I have selected "product1" and then enables the list. Anything else chosen should disable the list completely. List with ID product. So I guess it's something with my syntax for the option selected, not sure if thats the correct way to write it.
VBScript
'Disables or enables list based on selection
Function enabler()
For Each opt In document.GetElementByID("customer").Options
If opt.Selected = "product1" Then
document.GetElementByID("product").Disabled = False
Else
document.GetElementByID("product").Disabled = True
End If
Next
End Function
HTA
...
<select size="5" id="product" name="ListboxUserRole" onChange="SaveListboxUserRoleValue">
<option value="1" selected="selected">product1</option>
<option value="2">product2</option>
<option value="3">product3</option>
...
<select size="5" id="customer" name="ListboxCustomer" onChange="SaveListboxCustomerValue" value="1">
<option value="1" selected="selected">customer1</option>
<option value="2">customer2</option>
<option value="3">customer3</option>
<option value="4">customer4</option>
...

If I get it right, you need to enable the customer select when product1 is selected in product select, and disable if any other.
First of all, product select change event is linked to SaveListboxCustomerValue(). customer select disabling should be processed within SaveListboxCustomerValue(), replace <body onload="enabler()"> with just <body>.
IMO better to rework the algorithm to use the selectedIndex property of the product select object, the enabler() is not necessary then. Remove enabler(), and make changes to SaveListboxCustomerValue():
Sub SaveListboxCustomerValue()
' enable customer if the first item in product selected
customer.disabled = product.selectedIndex <> 0
' the rest part of the code
End Sub
Otherwise, if you want to keep the enabler(), then read about Option Object Properties. The condition you currently have always returns False, since boolean value never equals the string "product1". The code should be like this:
Sub enabler()
Dim opt
Dim match
For Each opt In product.options
match = opt.selected And opt.label = "product1"
If match Then Exit For
Next
customer.disabled = Not match
End Sub
enabler() should be called on customer change, add call to it:
Sub SaveListboxCustomerValue()
enabler()
' the rest part of the code
End Sub

Related

inspection of code shows code that should not be there or be there as it is

I'm working with an application written in dHTML and running on a Windows 10 laptop using the Apache server. I have some lines of simple code that generate a 2 SELECT statements as follows.
print " Choose an effort: <SELECT NAME='echoice' CLASS='slcts' SIZE='16' STYLE='width:180px' ONMOUSEOVER=\"setStatus2('Only efforts with measures your authorized to work with are shown.');\" ONMOUSEOUT=\"setStatus2(' ');\" ONCHANGE=\"Loadem('choice');\">\n";
foreach $iline (#ilines) {
print "<OPTION VALUE='$iline'>$iline</OPTION>\n";
}
print "</SELECT>\n";
print "Choose $what: <SELECT NAME='choice' CLASS='slcts' SIZE='16' STYLE='width:180px'>\n";
if ($cmfunction eq 'MeasureDescriptions' && $action eq 'U') {
print "<OPTION SELECTED VALUE='new'>new</OPTION>\n";
$imo = 1;
}
foreach $mline (#mlines) {
$thestr = ($i == 0 && !$imo) ? 'SELECTED' : '' ; # Select 1st measure if not a single model.
print "<OPTION $thestr VALUE='$mline'>$mline</OPTION>\n";
$i++;
}
print "</SELECT>\n";
I don't seem to be able to change the contents of this field as long as the first select is there. First I put a new unique character in the 2nd SELECT text each test to tell me if I'm executing the module version I think I am. I always see the unique character.
I have tried the following.
comment out the inner 2 print statements,
change >new< to >new1<,
assign 'dog' to the 0 position of the mlines array, and
add a print after the Option loop to add a 'dog' option.
In all four cases, the choice SELECT had the same options as it had before any of the tests (1-4) were implemented. But if I remove the first select,
I can then modify the options of the 2nd select. The debugger image of the above code follows with both selects.
Choose an effort: <SELECT NAME='echoice' CLASS='slcts' SIZE='16' STYLE='width:180px' ONMOUSEOVER="setStatus2('Only efforts with measures your authorized to work with are shown.');" ONMOUSEOUT="setStatus2(' ');" ONCHANGE="Loadem('choice');">
<OPTION SELECTED VALUE='all'>all</OPTION>
<OPTION VALUE='Health'>Health</OPTION>
<OPTION VALUE='HealthCare'>HealthCare</OPTION>
</SELECT>
Choose a measure !: <SELECT NAME='choice' multiple CLASS='slcts' SIZE='16' STYLE='width:180px' ONMOUSEOVER="setStatus2('Only measures your authorized to work with are shown. Select a measure for a formal definition.');" ONMOUSEOUT="setStatus2(' ');" ONCLICK='doFRef(event,this,this.form)'>
<OPTION SELECTED VALUE='new'>new</OPTION>
<OPTION VALUE='HEComp90'>HEComp90</OPTION>
<OPTION VALUE='HECompAtBirth'>HECompAtBirth</OPTION>
<OPTION VALUE='healthy'>healthy</OPTION>
<OPTION VALUE='infant'>infant</OPTION>
<OPTION VALUE='maternal'>maternal</OPTION>
<OPTION VALUE='stillbirths'>stillbirths</OPTION>
</SELECT>
The debugger code with only one select follows. I changed new to new1 and added the line and line2 options. But if I put the first select back in,
I can't modify the 2nd select and it contains what is shown in the 1st debugger image.
<OPTION SELECTED VALUE='new'>new1</OPTION>
<OPTION VALUE='line'>line</OPTION>
<OPTION VALUE='HEComp90'>HEComp90</OPTION>
<OPTION VALUE='HECompAtBirth'>HECompAtBirth</OPTION>
<OPTION VALUE='healthy'>healthy</OPTION>
<OPTION VALUE='infant'>infant</OPTION>
<OPTION VALUE='maternal'>maternal</OPTION>
<OPTION VALUE='stillbirths'>stillbirths</OPTION>
<OPTION VALUE='line2'>line2</OPTION>
</SELECT>
I don't understand this. Any help will be appreciated. It must be me.
Thanks,
craigt
It was me. There is an ONLOAD process that executed another process that preps
the content of that field that was not written correctly. I've corrected that process and all works as expected now. This question is closed. And I apologize for my oversight and thank everyone that took a look.

How to select particular option in a select list?

I'm trying to select option 2, but first it is selecting 2 then changing to 1.
This is the HTML of the select list:
<select id="IDITForm#additionalVehicleDrivers|1#youngestDriverExperienceVO#id">
<option style="background-color: rgb(252, 218, 175);" value="-1">Select</option>
<option value="1">0</option>
<option value="2">1</option>
<option value="3">2</option>
<option value="4">3</option>
<option value="5">4</option>
<option value="1000000">5</option>
<option value="1000001">more than 5</option>
</select>
This is the Watir code for selecting 2:
b.select_list(:id,"IDITForm#additionalVehicleDrivers|1#youngestDriverExperienceVO#id").select "2"
The problem is that you are using Watir-Classic's select method with a parameter that matches both an option's text as well as another option's value.
If you take a look at the code for the Watir::SelectList#select method:
def select(item)
matching_options = []
perform_action do
matching_options = matching_items_in_select_list(:text, item) +
matching_items_in_select_list(:label, item) +
matching_items_in_select_list(:value, item)
raise NoValueFoundException, "No option with :text, :label or :value of #{item.inspect} in this select element" if matching_options.empty?
matching_options.each(&:select)
end
first_present_option_value matching_options, :text
end
You can see that:
It gets a list of matching options based on text, label and value and
Then for each matching option Watir-Classic selects it.
For your specific example, this means that
2 options match the input "2":
<option value="2">1</option> matches since its value is "2"
<option value="3">2</option> matches since its text is "2"
Watir-Classic is selecting each of these options, in this specific order, which is why you see the dropdown switch to "1" and then "2"
Given the way the method is written and how your select list is written, you cannot use the select method. While the best choice is to move to Watir (previously Watir-Webdriver), there are workarounds in Watir-Classic.
Remove the ambiguity by specifically selecting an option based on only the value attribute:
id = "IDITForm#additionalVehicleDrivers|1#youngestDriverExperienceVO#id"
b.select_list(:id, id).select_value "3"
#=> will select <option value="3">2</option>
If you want to stick to selecting by text, directly locate/select the option element:
id = "IDITForm#additionalVehicleDrivers|1#youngestDriverExperienceVO#id"
b.select_list(:id, id).option(:text, "2").select
#=> will select <option value="3">2</option>
For Watir, you can use index based options where you need to worry about index only. Such as for:
<option value="2">1</option>
We can use,
browser.select_list(id: 'IDITForm#additionalVehicleDrivers|1#youngestDriverExperienceVO#id').options[2].select
As it is the 3rd index of given select_list and it follows zero based index ordering. Similarly for,
<option value="3">2</option>
We can use
browser.select_list(id: 'IDITForm#additionalVehicleDrivers|1#youngestDriverExperienceVO#id').options[3].select
As it is the 4th index of given select_list.
#select matches the text.
If you want to select by value you need to use #select_value:
b.select_list(id: "IDITForm#additionalVehicleDrivers|1#youngestDriverExperienceVO#id").select_value "2"

How to get an index of specific text in dropdown using vbscript code(.vbs)?

I am working on vbscript. I am writing script for selecting value from dropdown.
In my scenario, I want to select a value from dropdown using the text which resides in option tag i.e <option>value<option>
Request you to provide code for selecting value in dropdown using value not by index of value.
Thank you in advance. Please reffer below html code for reference.
<select id="mydropdown" onchange="getServiceDetails();" name="mydropdown">
value="[]"
<option value="-1">Select value</option>
value="[]"
<option value="0920102049">value 1</option>
value="[]"
<option value="0060217015">value 2</option>
</select>
This will do it.
Sub SelectOptionByInnerHTML(selectID, text)
Dim list, opt
Set list = document.getElementById(selectID)
For Each opt In list
If opt.innerHTML = text Then
opt.selected = true
Exit Sub
End If
Next
End Sub
Usage:
SelectOptionByInnerHTML "mydropdown", "value 2"

Issues in declaring variables

am trying to make a Petition form for a local company (A required project for graduation).
This is part of my page where I am facing troubles:
<FORM METHOD="POST" ACTION="InsertPetition.asp?EmpID=<%=Request.QueryString("EmpID")%>&BRANCH=<%=Session("BRANCH")%>" name=frmNewClaim>
<table>
<tr>
<td>From Date</td>
<td><SELECT name=cboFromDateDay>
<%For i = 1 To 31%>
<%if i=day(date()) then%>
<OPTION VALUE="<%=i%>" Selected="True"><%=i%></OPTION>
<%else%>
<OPTION VALUE="<%=i%>"><%=i%></OPTION>
<%end if%>
<%Next%>
</SELECT>
<SELECT name=cboFromDateMonth>
<%For i = 1 To 12%>
<%if i=month(date()) then%>
<OPTION VALUE="<%=i%>" Selected="True"><%=MonthName(i)%></OPTION>
<%else%>
<OPTION VALUE="<%=i%>"><%=MonthName(i)%></OPTION>
<%end if%>
<%Next%>
</SELECT>
<SELECT name=cboFromDateYear>
<%For i = Year(Date())-1 To Year(Date()) + 10%>
<%if i=year(date()) then%>
<OPTION VALUE="<%=i%>" Selected="True"><%=i%></OPTION>
<%else%>
<OPTION VALUE="<%=i%>"><%=i%></OPTION>
<%end if%>
<%Next%>
</SELECT>
</td>
<td><INPUT type="button" value="Submit" name=cmdSubmit></td>
</tr>
</table>
<SCRIPT LANGUAGE="VBSCRIPT">
Sub cmdClose_OnClick()
window.close
End Sub
Sub cmdSubmit_OnClick()
Dim fromdate2
Dim todate2
fromdate2="Trim(frmNewClaim.cboFromDateDay.Value) & "/" & Trim(frmNewClaim.cboFromDateMonth.Value) & "/" & Trim(frmNewClaim.cboFromDateYear.Value)"
todate2="Trim(frmNewClaim.cboToDateDay.Value) & "/" & Trim(frmNewClaim.cboToDateMonth.Value) & "/" & Trim(frmNewClaim.cboToDateYear.Value)"
Select case frmNewClaim.txtPTNGroup.Value
'If type is Birth
case 30
if DateDiff("d",fromdate2,todate2) > 45 then
msgbox "Days value exceeded the limit of days allowed!",16,"Data Entry Error"
frmNewClaim.txtPTNGroup.focus
Exit Sub
end if
case else
End Select
frmNewClaim.submit
End Sub
In this form, the user is required to insert day, month, year in the from date and the to date table row after selecting the type of the petition for example : Birth
In the script, i declared variables fromdate2 and todate2which are supposed to contain the date for example like this: "03/12/2015"
and in the select case case 30, the datediff function which will give me the amount of days between the fromdate2 todate2 and if it is less then 45 then a message box would appear (yes, i know it only works on IE) say the days exceeded the limit
The problem is when i test it and click submit nothing happens
Am pretty sure the error is in the fromdate2 todate2 variables
Help me and thank you in advance
I'm confused as to how you are trying to collect your form data. Unless I'm missing something you appear to be mixing server side and client side code.
Classic ASP is a server side technology, VBScript is the language it usually uses, (you can specify JavaScript if you prefer.)
Your subroutines are contained within <SCRIPT LANGUAGE="VBSCRIPT">. This means that it will be run client side - and as you are aware it will only work on IE
I think what you need to do is to first move your code to the server side. You can do this either by putting it inside <% %> delimiters, or by using <SCRIPT LANGUAGE="VBSCRIPT" runat="server">
Instead of onclick events use the request object to populate variables retrieved from your form fields, eg cboFromDateDay = Request.Form("cboFromDateDay")
Finally your vbs script seems designed to generate popup alerts. This wouldn't work with server side code. You could use Response.Write to have error messages appear within the text of your page.
If you desperately want popup alerts then you could have them triggered when the page loads, and put the code which triggers them within an asp conditional statement so that it only appears in the server output if certain conditions are met. I would suggest though that this is more trouble than it's worth

VB Script to select a dropdown value, without knowing it's technical ID

I need to create a vb Script which would select a value from a drop down selection box.
Problem is I just know the name of the window; no technical ID's of the window or dropdown is known.
I can only bring it in front through "AppActivate".
I have also tried using "SendKeys", but it is not an editable drop down menu, so simply entering the value does not help.
Could you please help.
Regards,
Suyash Rathi.
Too much unknown staffs, but here is something to get started.
Lets say we have this code in our hmtl page:
<select>
<option value="A">Volvo</option>
<option value="B">Saab</option>
<option value="C">BMW</option>
<option value="D" selected>Audi</option>
</select>
In .vbs script add:
Set IE = WScript.CreateObject("InternetExplorer.Application", "IE_")
IE.Visible = True
IE.Navigate "http://your_target_url_here.com/"
Do
WScript.Sleep 100
Loop While IE.ReadyState < 4 And IE.Busy
' get first HTMLSelectElement object:
Set e = Document.getElementsByTagName("select")(0)
' just for undestanding...
MsgBox e.Options(e.selectedIndex).Value '-> "D"
MsgBox e.Options(e.selectedIndex).Text '-> "Audi"
' select first option:
e.selectedIndex = 0

Resources