Click here to Skip to main content
15,887,344 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to programmatically add a button element to an HTA. I wrote the following code as a test. The button, "Button1", gets added to the HTA window, but when I click it, it doesn't run the "subButtonClickedMsg" subroutine. No error messages are produced.

I added the other two buttons ("Button2" and "Button3") to try to figure out what was wrong.
"Button2" calls the "subButtonClickedMsg" subroutine with no issue.
"Button3" calls the "subChangeButton1ValueAttribute" subroutine which:
1) Changes the value attribute of "Button1" to "Button1 Value Changed"
2) Changes the "onclick" attribute to "subButtonClickedMsg" (which is what it was initially anyway)
3) Displays a MsgBox to prove that the "onclick" attribute is set to "subButtonClickedMsg".

When I click on "Button3", the code performs as expected. However, when I click on "Button1" after clicking "Button3", "Button1" still fails to run the "subButtonClickedMsg" subroutine. Again, no error messages are produced.

I did a Google search and turned up some info on the "addEventListener" method. So I tried adding this code to the "subChangeButton1ValueAttribute" subroutine to test it:

document.getelementById("Button1").addEventListener "onclick", "subMyButtonOnClick", false

but it produced the following script error when I clicked on "Button3":

Script Error
Object doesn't support this property or method:
'document.getelementById(...).addEventListener'

According to this link on MSDN, "addEventListener" should be supported:

https://msdn.microsoft.com/en-us/library/ff975245(v=vs.85).aspx

I've commented the line for now.

What am I missing in trying to programmatically add the button that is causing its onclick event to not execute the specified subroutine?

Thanks in advance for any help.

What I have tried:

<html>
  <head>
    <title>HTATest2</title>

     <HTA:APPLICATION 
     ID="objHTATest"
     APPLICATIONNAME="HTATest"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
     WINDOWSTATE="maximize"
     >
  </head>

  <script type="text/vbscript">

    Dim myButton

    Sub subButtonClickedMsg
      MsgBox "Button has been clicked."
    End Sub 'subButtonClickedMsg

    Sub subChangeButton1ValueAttribute
      'document.getelementById("Button1").addEventListener "onclick", "subMyButtonOnClick", false
      myButton.setAttribute "value","Button1 Value Changed"
      myButton.setAttribute "onclick","subButtonClickedMsg"
      MsgBox "myButton.getAttribute(""onclick"") = " & myButton.getAttribute("onclick")
    End Sub 'subChangeButton1ValueAttribute

    Sub Window_Onload()
      Set myButton = document.createElement("input")
      myButton.setAttribute "type","button"
      myButton.setAttribute "id","Button1"
      myButton.setAttribute "onclick","subButtonClickedMsg"
      MsgBox "myButton.getAttribute(""onclick"") = " & myButton.getAttribute("onclick")
      myButton.setAttribute "value","Button1"
      document.getElementById("myBody").appendChild(myButton)
    End Sub 'Window_Onload()

  </script>
  <body id=myBody>
    <input type=button id=idButton2 value=Button2 onclick=subButtonClickedMsg />
    <input type=button id=idButton3 value="Button3" onclick=subChangeButton1ValueAttribute />
  </body>
</html>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900