'Can't programmatically change <input>'s value
I'm working on a few TradingView strategies on my spare time, and most of these strategies can be configured via the Inputs tab in the Configurations window.
For some reason I'd like to be able to change these inputs' values programmatically via JavaScript. I've managed to make it work for checkboxes using input.click(), but I haven't yet found a way to change the value of a text input so that the change is picked up by TradingView.
For example if you select a text input via the browser's inspection tool
and then type in the console
$0.value = 50
you'll notice that the input is visually updated and now shows 50 but the strategy's chart and the Strategy Tester's data are not. Also, if you hover the input with your mouse you'll see its value be reset to the original value (8). Which means the change wasn't picked up by TradingView.
Does anyone knows how to work around this?
Solution 1:[1]
Most likely the page is listening to events on the input and updating things when they fire. By directly setting the value you are not firing any of the events.
There are potentially three events I can think of off hand that it might be listening to, depending on the developer.
Try the following to see if any of them work
$0.dispatchEvent(new Event('change'));
$0.dispatchEvent(new Event('keydown'));
$0.dispatchEvent(new Event('keyup'));
Solution 2:[2]
I did something similar with SignalR on ASP.NET (not Core). I overloaded the OnConnected and OnDisconnected methods in the Hub class to keep my own Dictionary of connected clients.
Each client has a header with their login information. In OnConnected, I would parse the header and check if this client is already connected. If they are not, I would add them to the dictionary. In OnDisconnected, I would check if this client is connected and if they are, remove them from the dictionary. This way, the dictionary always has the connected clients.
Note: because a new Hub is created for every new login, the dictionary has to be a singleton or static. I use a static ConcurrentDictionary.
Note 2: SignalR has an internal structure for storing all clients but it is not possible to easily search without modifying the library and has very limited public methods. This is why I used my own Dictionary.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Deadron |
| Solution 2 |

