0

I am trying to play with some data insertion on forms of this website. I can manually put my cursor on inputs and type digits, such as 999. If I reload the page, the value that I typed is still there.

I can access this value by using Google Chrome console (on dev tools) and using the element ID OSICtrlE1-23:

> document.querySelector("#OSICtrlE1-23").value 
'999'

Ok. Apparently, until some point, I can also mutate this value using JS and the console:

> document.querySelector("#OSICtrlE1-23").value = 888

It seems to work on the UI. I can see the new value on the UI.

Also, it seems to work on the console when I request the value on the console with JS:

document.querySelector("#OSICtrlE1-23").value 
'888'

Unfortunately, if I refresh the page, the value on the UI goes back to 999 (which was the previous value manually inserted).

1 - Why is this happening?

2 - Could the website be restricting what I can do with the console? If so, how to know?

3 - Which command on the console should I be using in order to have the data inserted by computation behaving exactly as the data manually inserted?

3
  • 1
    This is most likely related to state or a cache of some kind. Directly editing an element's value via dev-tools bypasses many of the events usually associated with inputs (onChange and the like). Commented Sep 17, 2022 at 0:24
  • Thanks, @Yulian. And how to solve this? Is it possible? Commented Sep 17, 2022 at 1:29
  • 1
    I've never heard of a use-case like this before, my guess would be it's not. You can try using the React Dev Tools extension in Chrome/Firefox to directly mess with components/methods Commented Sep 17, 2022 at 2:45

2 Answers 2

2

@Yulian was correct. After carefully inspecting how events were being handled, a reverse engineering endeavor found out that blur was the key. Hence, dispatching the event did the trick!

See the code that works as expected:


const updateInput = (value) => {
   const input = document.querySelector("#OSICtrlE1-23");
   input.value = value;
   input.dispatchEvent(new Event('blur'));
}

Sign up to request clarification or add additional context in comments.

Comments

1

the secret behind dispatching the right event , @Pedro's solution works for most cases but this solution worked for me

element.dispatchEvent(new Event('input', {bubbles:true}));

Source How do I programmatically trigger an “input” event without jQuery?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.