0

I have an input field with a default value of $1. I want to change that value to say $5 and then click a button. I used the following code to try to achieve this:

function run() {
  var trade_amount = document.querySelectorAll('.value__val input')[0];
  trade_amount.value="";
  trade_amount.value = '$5';
  trade_amount.blur();

  const call_button = document.getElementsByClassName("btn btn-call")[0];
  call_button.click();
}

The issue is that every time the button is clicked, the $5 or any value I put into the input field changes back to the default $1 before clicking. So, it clicks with the default value rather than the value I changed it to. How do I solve this issue?

  • The line `trade_amount.value="";` doesn't do anything here. – kelsny Jan 05 '23 at 19:51
  • If you only want the first element, why not use `querySelector` instead? Similarly, you can use `querySelector(".btn.btn-call")` instead of `getElementsByClassName`. – kelsny Jan 05 '23 at 19:52
  • you forgot to close your function with a `}` – Chris G Jan 05 '23 at 19:52
  • How is `run()` invoked, from where, under what circumstances? and show at least some of the relevant HTML. (and I don't see any jquery in your code) – Stephen P Jan 05 '23 at 19:57
  • `call_button.click();` is the part of this that stands out to me; what is that button click for? Is there any chance it's what's resetting your form back to default values? – Daniel Beck Jan 05 '23 at 20:37
  • @DanielBeck, I am trying to automate a forex trading site and the button is supposed to be click to enter in a trade. The default trading amount is $1 and I am trying to make it trade $5. – donaldekpe Jan 05 '23 at 20:50
  • @ChrisG The missing } is a copy and paste mistake. It is actually included in the original code. – donaldekpe Jan 05 '23 at 20:53
  • @DanielBeck The button does not reset the field, it just enters in the trade. – donaldekpe Jan 05 '23 at 21:00
  • "a copy and paste mistake. It is actually included in the original code" — well, then edit your question and fix it. – Stephen P Jan 05 '23 at 21:26

1 Answers1

-1

You're probably setting the value back to "$1" somewhere else in your code (to fix this, I'd need to know where you're setting that default value). But, if you want a quick hacky workaround, you can try using the setTimeout function to set the value after all the other possible setters/event listeners have already executed):

function run() {
  var trade_amount = document.querySelectorAll('.value__val input')[0];
  trade_amount.value="";
  setTimeout(() => { trade_amount.value = '$5'; }, 0)
  trade_amount.blur();

  const call_button = document.getElementsByClassName("btn btn-call")[0];
  call_button.click();
}
  • The function run() is the only function in the code and so there is there can't be anywhere else in the code that is setting the value back to $1. – donaldekpe Jan 05 '23 at 21:05
  • Where do you declare the default value of `$1`? – Live bug help - www.dialect.so Jan 05 '23 at 21:06
  • I am trying to create a bot for an existing forex trading site and the default trading amount of the site is $1, so I am not the one setting the default amount. I am only trying to change the value to $5 and then click the trade button to enter $5 instead of $1. – donaldekpe Jan 05 '23 at 21:07
  • I have tried the solution you provided, unfortunately it did not work. – donaldekpe Jan 05 '23 at 21:10
  • So, my guess is that they probably use a framework like React to manage the state of the input box, where React contains the expected value in the input box and only updates it when the `onChange` event is triggered. You might need to manually trigger the `onChange` function after updating the input, see https://stackoverflow.com/questions/2856513/how-can-i-trigger-an-onchange-event-manually – Live bug help - www.dialect.so Jan 05 '23 at 21:11