Getting value from html button inside gui Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Mipha
Posts: 54
Joined: 27 Jul 2018, 17:08

Getting value from html button inside gui

16 Aug 2018, 10:04

This is what I got so far. I want to retrieve the value from a button when I press it. How should I go about doing that?

Code: Select all

#Singleinstance force

html=
(
<!DOCTYPE html>
 <html>
  <head>
   <style type="text/css">
    html {overflow:hidden}
    body {background:#AAAAAA;}
    .first {background: #AAAAAA; color: #111111;}
    .second {background: #111111; color: #AAAAAA;}
   </style>
  </head>
  <body>
   <button class='first' id='stuff' value='1'>Button 1</button>
   <button class='second'id='stuff' value='2'>Button 2</button>
  </body>
 </html>
)

Gui,Margin,0,0
Gui, Add, ActiveX, W400 H300 x0 y0 vdoc, HTMLFile
doc.write(html)
add_ButtonHandler()
gui, show
return

add_ButtonHandler() {
    global
    Loop % doc.getElementsByTagName("Button").length
        ComObjConnect(b_%A_Index%:=doc.getElementsByTagName("Button")[A_Index-1], b_%A_Index%.GetAttribute("Id") "_")
}

stuff_OnClick() {
    ;get the contents of the values from a button here
    MsgBox A
}
A_AhkUser
Posts: 879
Joined: 06 Mar 2017, 16:18
Location: France

Re: Getting value from html button inside gui  Topic is solved

21 Aug 2018, 14:35

Code: Select all

#NoEnv
#Singleinstance force
#Warn

html =
(LTrim Join C
<!DOCTYPE html>
<html>
<head>
	<style type="text/css">
	html {
		overflow: hidden;
	}
	body {
		background: #AAAAAA;
	}
	.first {
		background: #AAAAAA;
		color: #111111;
	}
	.second {
		background: #111111;
		color: #AAAAAA;
	}
	</style>
</head>
<body>
	<button class='first' id='stuff1' value='1'>Button 1</button>
	<button class='second' id='stuff2' value='2'>Button 2</button>
</body>
</html>
)
GUI, Margin, 0, 0
GUI, Add, ActiveX, W400 H300 x0 y0 vHTMLDoc, about:<!doctype html><meta http-equiv="X-UA-Compatible" content="IE=edge">
HTMLDoc.document.write(html), HTMLDoc.document.close()
GUI, Show, AutoSize
ComObjConnect(_:=HTMLDoc.document, new Events)
return

Class Events {
    onClick() {
		global HTMLDoc
        _target := HTMLDoc.document.parentWindow.event.srcElement
		ToolTip % "Value attribute > " . _target.getAttribute("value") . " `r`n innerText >" . _target.innerText
    }
}
Mipha
Posts: 54
Joined: 27 Jul 2018, 17:08

Re: Getting value from html button inside gui

22 Aug 2018, 18:05

Thanks for the answer. I've been stuck on this one for a while.
A_AhkUser
Posts: 879
Joined: 06 Mar 2017, 16:18
Location: France

Re: Getting value from html button inside gui

22 Aug 2018, 19:11

I'm glad I could help ;) Btw, I used the getAttribute method; its main advantage is that it does not notify of COM errors if the property could not been accessed, regardless the use of ComObjError. Also, note that I used event.srcElement, which is IE-specific (or, at least, I think). You can off course use alternatively event.target.

Return to “Ask For Help”

Who is online

Users browsing this forum: colt and 33 guests