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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onchange event handler problem

Status
Not open for further replies.

RMonty

Technical User
Aug 15, 2002
3
US
Hi

I'm pretty new to VBScript and I'd appreciate some help with the following issue. I want to create an event handler for the onchange event in a select tag. I need the selectedIndex and option value when a listbox selection is changed. The list box is loaded from an access db and this seems to be working fine. I created a simple script to provide an alert containing the information of interest when the selection changes. The alert works when coded in line in the select tag. The alert does not work when i call an event handler.

Here are the code fragments:

This works:
Code:
<select name="StudyId" size="1" id="StudyId" onchange="alert('Index: ' + this.selectedIndex + '\nValue: ' + this.options(this.selectedIndex).value)">

This works too:
Code:
<Script language=VBScript>
Sub StudyId_onchange()
 alert("Hello from selected")
End Sub
</Script>

.../header, body, and form tags go here
Code:
<select name="StudyId" size="1" id="StudyId" onchange="StudyId">

This does not work:
Code:
<Script language=VBScript>
Sub StudyId_onchange()
  alert("Index: " + this.selectedIndex + "\nValue: " + this.options(this.selectedIndex).value)
End Sub
</Script>
...

Code:
<select name="StudyId" size="1" id="StudyId" onchange="StudyId">

Why is the alert in the 2nd event handler not working?
I have tried passing a parameter in the call as in StudyId(this), and changed the alert msgtext to use the parameter, I've also tried to formally define the parameters in the alert msgtext as in form1.StudyId.selectedIndex. Neither of these options worked for me.

Any help would be greatly appreciated.

Thanks.
 
Hello RMonty,

It's me in vbscript as this in javascript.
[tt]
Sub StudyId_onchange(obj)
alert("Index: " + obj.selectedIndex + "\nValue: " + obj.options(obj.selectedIndex).value)
End Sub
[/tt]
with calling like:
[tt]
<select name="StudyId" size="1" id="StudyId" onchange="StudyId(me)">
[/tt]
This is not the only way to do it.

regards - tsuji

 
Hi tsuji,

Thanks for your reply. I tried the substitution of me for this as you described (see code below). The alert still does not come up.

Code:
<Script language=VBScript>
Sub StudyId_onchange(obj)
  alert("Index: " + obj.selectedIndex + "\nValue: " + obj.options(obj.selectedIndex).value)
End Sub
</Script>
and the event handler
Code:
<select name="StudyId" size="1" id="StudyId" onchange="StudyId(me)">

Alternative solution suggestions would be appreciated.

Thanks again for your help.
 
First of all, could you tell me why you are using VBScript in HTML document? The reason of this question is primarly that in addition to being not standard (only supported by IE), I saw somewhere on press releases that they plan to stop to support it in web pages.

Anyway, here are 2 ways of doing it. As you will surely see, I replaced the alert method by msgbox (which is the real equivalent in VBScript). Also, I replaced + by & and \n by vbCrLf (again, the equivalents in vb)

As for the usage, the first method (StudyId_onchange) is implicitly linked to the object, you don't have to specify the onchange event in the object (if you do, it will be called 2 times). The second one is if you use the onchange event with me being passed as the parameter.

Code:
<html>
<head>
	<Script language=VBScript>
		Sub StudyId_onchange(e)
			dim obj
			set obj = e.srcElement
			msgbox("Index: " & obj.selectedIndex & vbCrLf & "Value: " + obj.options(obj.selectedIndex).value)
		End Sub
		
		Sub myOnChangeHandler(obj)
			msgbox("Index: " & obj.selectedIndex & vbCrLf & "Value: " + obj.options(obj.selectedIndex).value)
		end sub
	</Script> 
</head>
<body>
	<form>
		<select name="StudyId" size="1" id="StudyId" onchange="myOnChangeHandler(me)">
			<option value=1>a</option>
			<option value=2>2</option>
			<option value=3>3</option>
		</select>
	</form>
</body>
</html>

Hope this clarifies a bit.
 
RMonty,

I forgot to edit out the alert and replace it with msgbox.

- tsuji
 
Further note:

And have to replace \n by vbcrlf. Hence the alert line should be read as:
[tt]
msgbox "Index: " & obj.selectedIndex & vbcrlf & "Value: " & obj.options(obj.selectedIndex).value
[/tt]
Sorry for the confusion.

- tsuji
 
Hi

Thanks for all the help. It turned out that the test server site was corupt somehow. The asp component bombed which had been working. When we recreated the site everything worked fine. Me worked, both types of event handlers (passing me,or implicitly using me in the handler). I really appreciate all the input.

Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top