Thread: Text area
View Single Post
  #5 (permalink)  
Old September 18th, 2008, 02:39 PM
Old Pedant Old Pedant is offline
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Why do you say "couldn't work"??? Seems to me it worked fine. Except your code doesn't handle the case when two values are the same and also the least.

Anyway, "me" (or "ME" or "Me") is a special keyword in VBScript that means "the current object". So in an ONCLICK handler, "me" refers to the object that was clicked on...the button.

Your code could be a lot shorter and could be written to handle invalid data from the form fields. Try this:
Code:
<HTML>
<HEAD>
<script language="VBScript">
Sub Compare( aField )
    Dim frm, n0, n1, n2, least, evenOdd
    Set frm = aField.form
    n0 = 1E99 : n1 = 1E99 : n2 = 1E99
    On Error Resume Next
        n0 = CLNG( frm.nom0.value )
        n1 = CLNG( frm.nom1.value )
        n2 = CLNG( frm.nom2.value )
    On Error GoTo 0
    If n0 <= n1 AND n0 <= n2 Then
       least = n0
    Elseif n1 <= n0 AND n1 <= n2 Then
       least = n1
    Else
       least = n2
    End If
    If least > 1E20 Then 
         frm.answer.value = "No valid numbers were entered...try again"
         Exit Sub
    End If
    If least MOD 2 = 0 Then evenOdd = "even" Else evenOdd = "odd"
    frm.answer.value = least & " is the smallest and it is " & evenOdd
End Sub
</script>
</HEAD>
<BODY>
<FORM>
<TABLE BORDER="0">
<TR>
    <TD>Enter a Number</TD>
    <TD><INPUT NAME="nom0" SIZE="10" onChange="Call Compare(me)"></TD>
</TR>
<TR>
    <TD>Enter a Number</TD>
    <TD><INPUT NAME="nom1" SIZE="10" onChange="Call Compare(me)" ></TD>
</TR>
<TR>
    <TD>Enter a Number</TD>
    <TD><INPUT NAME="nom2" SIZE="10" onChange="Call Compare(me)"></TD>
</TR>
<TR>
    <TD align=right>Answer:</TD>
    <TD><INPUT NAME="answer" SIZE="50" readonly onFocus="Call me.form.nom0.focus()"></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
Reply With Quote