Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a script which would be able to solve a math problem and after that use the button ids to enter the solution.
var a = document.getElementById("txtTehtav").textContent.match(/\d+/g)
let [x, y] = a
var answer = x*y

This part of the code takes the problem (for example 33x40) and splits it in a way that 33 will be x and 40 will be y. After that it will calculate it and then the variable will be the answer.
Now from here on I want to make it input the answer. I know the ids to the buttons (buttons range from 0-9, with ids of num(1), num(2) and so on. I want my script to split the answer (in this case it would be 1320, so it would split it into 1(o); 3(p); 2(q); 0(r) and enter it with
num(1) num(3) num(2) num(0) num(OK)

How can this be done? Any help is appreciated!

What I have tried:

Nothing, I'm not really sure on how to solve this.
Posted
Updated 22-Apr-21 11:12am

1 solution

- First, your buttons each need an id and the button's numerical value could be it (like id='8')
- Next, you convert you string in whatever manner you wish to a list of the individual character.
- Then you give each button a click event to do whatever it is you want to do.
- Finally, loop through the list and execute the click events in code

Now all of this can be found, depending upon where you short fallings in this are, at W3Schools Online Web Tutorials[^] as various tutorial which you may need: HTML, JavaScript, DOM .
 
Share this answer
 
Comments
Richard Deeming 23-Apr-21 4:38am    
Interesting - I was ready to point out that the ID can't start with a number, but it seems that was an HTML4 rule which has been relaxed in HTML5. :)

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