TalkingScientist
TalkingScientist

Reputation: 93

Dynamically creating button in HTA causes undefined function onClick

I needed a small HTA to give a couple list boxes and submit button that would kick off some SQL bits. All was well when the HTML portion was static but when I try to make it dynamic so that current month/year can be default in the drop downs the code quits working and says that my ButtonClick is undefined. Here is a simplified version of the code. I've tried just using btn01_OnClick, ButtonClick() and a few other fruitless variations. Thoughts?

<HEAD>
  <TITLE>Drop Down Menu</TITLE>
  <HTA:APPLICATION ID="oMyApp" 
    APPLICATIONNAME="Drop Down" 
    BORDER="Dialog"
    CAPTION="Yes"
    SCROLL="NO"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="Yes"
    WINDOWSTATE="maximize">
</HEAD>

<SCRIPT LANGUAGE="VBScript">

Sub ButtonClick
        Document.write ("Success")
End Sub


Sub Window_OnLoad
strHTML = strHTML & "<BODY><SPAN>"
strHTML = strHTML & "<H2>Select Month</H2>"
strHTML = strHTML & "<P>Select Month: "
strHTML = strHTML & "<SELECT NAME=""Month"">"
    strHTML = strHTML & "<OPTION selected>" & MonthName(Month(Date),False) & "</OPTION>"
    strHTML = strHTML & "<OPTION>January</OPTION>"
strHTML = strHTML & "</SELECT><P>"

strHTML = strHTML & "<P>Select Year: "
strHTML = strHTML & "<SELECT NAME=""Year"">"
    strHTML = strHTML & "<OPTION selected>" & Year(Date) & "</OPTION>"
    strHTML = strHTML & "<OPTION>2014</OPTION>"
strHTML = strHTML & "</SELECT><P>"

strHTML = strHTML & "<BR><BR>"
strHTML = strHTML & "<Input Type = " & Chr(34) & "Button" & Chr(34) & " Name = " & Chr(34) & "btn01" & Chr(34) & " onClick = " & Chr(34) & "ButtonClick" & Chr(34) & " VALUE = " & Chr(34) & "SUBMIT" &  Chr(34) & ">"

strHTML = strHTML & "<BR><BR></SPAN>"
strHTML = strHTML & "</BODY>"
Document.write(strHTML)
    Window.Month.Focus
End Sub

</SCRIPT>

Upvotes: 0

Views: 874

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200373

Your script section should be part of the <head> section, and I'd use

Document.body.innerHtml = strHTML

instead of

Document.write strHTML

Example:

<html>
<head>
  <title>Drop Down Menu</title>
  <HTA:APPLICATION ID="oMyApp"
    ...
    WINDOWSTATE="maximize">

<script language="VBScript">
Sub ButtonClick
  Document.write "Success"
End Sub

Sub Window_OnLoad
  strHTML = "<SPAN>"
  ...
  strHTML = strHTML & "<BR><BR></SPAN>"
  Document.body.innerHtml = strHTML
  Window.Month.Focus
End Sub
</script>
</head>

<body>
</body>
</html>

Upvotes: 1

Related Questions