Possible timing problem in onclick

G

Guest

I have a little vbScript that is invoked by onclick in
one of the option lines of a select structure. With the
msgbox in the script, it works fine and returns the index
of the option clicked. With msgbox deleted (or commented
out), it always returns -1.

Any suggestions?

Gerry Metze



<script language="vbscript">
Sub selNm
ix = -1

msgbox "in selNm"

For i = 0 To formNx.selNx.Length - 1
If formNx.selNx.Options(i).Selected _
= True Then ix = i
Next
navigate("QSelect.asp?HP=2&NMix=" & ix)
End Sub
........
<body>
<form name="Nx">
<fieldset>
<select align="left" multiple size="15" name="selNx"
onclick="selNm">
<% For i = 1 To UBound(VV)
Response.Write "<option
onclick=""selNm2"">" & VV(i) & "</option><br>"
Next %>
</select>
</fieldset>
</form>
 
L

Lisa Wollin \(Microsoft\)

Hi, Gerry,

On a quick glance through, in the sub you are using "formNx" as the form
name, but in the opening Form tag, the name attribute is set to "Nx". Try
changing this so that the script reads

For i = 0 To Nx.selNx.Length - 1
If Nx.selNx.Options(i).Selected _
= True Then ix = i
Next
navigate("QSelect.asp?HP=2&NMix=" & ix)

However, there may also be another problem. In the Select tag you include
the multiple attribute, which allows users to select more than one item.
However, when the For loop runs, only the last selected item would end up
becoming the value of ix. This may be fine, but just in case, you may want
to remove the multiple from the select tag. Also, you can probably forgo
the For loop and use the selectedIndex property, which would return an
integer of the selected item, assuming, of course, if only one item was
selected. In this case, you could safely replace the For loop with the
following code:

ix = Nx.selNx.selectedIndex

--
Lisa Wollin
Programmer Writer
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included code samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
 
G

Gerry Metze

Hi Lisa,
-----Original Message-----
Hi, Gerry,

On a quick glance through, in the sub you are using "formNx" as the form
name, but in the opening Form tag, the name attribute is set to "Nx". Try
changing this so that the script reads

For i = 0 To Nx.selNx.Length - 1
If Nx.selNx.Options(i).Selected _
= True Then ix = i
Next
navigate("QSelect.asp?HP=2&NMix=" & ix)

However, there may also be another problem. In the Select tag you include
the multiple attribute, which allows users to select more than one item.
However, when the For loop runs, only the last selected item would end up
becoming the value of ix. This may be fine, but just in case, you may want
to remove the multiple from the select tag. Also, you can probably forgo
the For loop and use the selectedIndex property, which would return an
integer of the selected item, assuming, of course, if only one item was
selected. In this case, you could safely replace the For loop with the
following code:

ix = Nx.selNx.selectedIndex

--
Lisa Wollin
Programmer Writer
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included code samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.



.
 
G

Guest

Hi Lisa,
-----Original Message-----
Hi, Gerry,

On a quick glance through, in the sub you are using "formNx" as the form
name, but in the opening Form tag, the name attribute is set to "Nx". Try
changing this so that the script reads

For i = 0 To Nx.selNx.Length - 1
If Nx.selNx.Options(i).Selected _
= True Then ix = i
Next
navigate("QSelect.asp?HP=2&NMix=" & ix)

However, there may also be another problem. In the Select tag you include
the multiple attribute, which allows users to select more than one item.
However, when the For loop runs, only the last selected item would end up
becoming the value of ix. This may be fine, but just in case, you may want
to remove the multiple from the select tag. Also, you can probably forgo
the For loop and use the selectedIndex property, which would return an
integer of the selected item, assuming, of course, if only one item was
selected. In this case, you could safely replace the For loop with the
following code:

ix = Nx.selNx.selectedIndex

--
Lisa Wollin
Programmer Writer
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included code samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.



.
 
G

Guest

Hi Lisa,
-----Original Message-----
Hi, Gerry,

On a quick glance through, in the sub you are using "formNx" as the form
name, but in the opening Form tag, the name attribute is set to "Nx". Try
changing this so that the script reads

For i = 0 To Nx.selNx.Length - 1
If Nx.selNx.Options(i).Selected _
= True Then ix = i
Next
navigate("QSelect.asp?HP=2&NMix=" & ix)

However, there may also be another problem. In the Select tag you include
the multiple attribute, which allows users to select more than one item.
However, when the For loop runs, only the last selected item would end up
becoming the value of ix. This may be fine, but just in case, you may want
to remove the multiple from the select tag. Also, you can probably forgo
the For loop and use the selectedIndex property, which would return an
integer of the selected item, assuming, of course, if only one item was
selected. In this case, you could safely replace the For loop with the
following code:

ix = Nx.selNx.selectedIndex

--
Lisa Wollin
Programmer Writer
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included code samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.



.
 
G

Guest

Hi Lisa,
Thanks a lot. I think the problem was the "multiple"
tag which was left over from a previous incarnation of
the program (silly me). I also appreciate your
suggestion to use "selected Index" instead of the loop,
which makes for a much more elegant program.
 
G

Guest

(This is the fourth attempt to post a somewhat lengthy
reply -- it gets posted before I'm ready without any
action on my part.) Anyway, to continue: I am still
mystified why the script worked WITH the msgbox, but
didn't without it -- must have been the multiple tag. In
any case, the program now works as intended. ANd in case
the previous replies didn't make it, thanks for the
prompt reply and the useful hint
regarding "selectedIndex".

Gerry Metze
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top