Using variable inside function

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
boton
Posts: 8
Joined: 13 Oct 2019, 22:41

Using variable inside function

31 Mar 2020, 12:46

Code: Select all

target := wb.document.querySelector("SELECT[id=""option123-field""]")
target.value := "123456"
event := wb.document.createEvent("Event")
event.initEvent("change", true, true)
target.dispatchEvent(event)
If I hard code this and run the script it will be run perfectly but time to time the "option123-field" will change which will cause the script to not run properly. What I did next was extract the id from the OuterHTML using SubStr

Code: Select all

IDString := wb.document.GetElementsByTagName("option")[1].OuterHTML
ID := SubStr(ID, 31, 9)
IDselector = SELECT[id="%ID%"]
I believe what I have done so far is correct so usingIDselector I tried to put this into the .querySelector like so

Code: Select all

target := wb.document.querySelector(IDselector)
target.value := "123456"
event := wb.document.createEvent("Event")
event.initEvent("change", true, true)
target.dispatchEvent(event)
But when I run the program I am getting a error of variable name contains illegal character, any input on how to do this correctly would be great. Thank you
User avatar
boiler
Posts: 17390
Joined: 21 Dec 2014, 02:44

Re: Using variable inside function

31 Mar 2020, 13:07

boton wrote:
31 Mar 2020, 12:46

Code: Select all

IDString := wb.document.GetElementsByTagName("option")[1].OuterHTML
ID := SubStr(ID, 31, 9)
IDselector = SELECT[id="%ID%"]
You want to take the SubStr of IDString. Should be:

Code: Select all

ID := SubStr(IDString, 31, 9)
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: Using variable inside function

31 Mar 2020, 20:13

Also, querySelector can look up an element more simply by prefixing the id with a #.

Code: Select all

Document.querySelector(“#option123-field”)
boton
Posts: 8
Joined: 13 Oct 2019, 22:41

Re: Using variable inside function

02 Apr 2020, 12:18

Code: Select all

<select tabindex="0" id="option123-field" aria-describedby="option123-messages"><option class="ui-option" id="option1955" value="">Choose an account</option>	<option class="ui-option" id="option111" value="abc123">Chequing </option>
	<option class="ui-option" id="option222" value="abc456">Savings</option>
</select>
Might be easier if I posted up the code and get some input on how I could better approach this problem.

I was selecting and changing the option value based in the id="option123-field" using .querySelector() but since the id value kept changing I'm having a hard time using the extracted id value for the .querySelector() to make it change and select the Chequeing option

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], MrHue, Rohwedder, Rxbie, songdg and 361 guests