M
M
myxasa2017-06-15 10:16:09
Frontend
myxasa, 2017-06-15 10:16:09

How to change reactjs input value?

It is necessary to execute js code from the chrome console (without any react debuggers) that will change the value of a certain field. The application is written in react.
Here is a similar question 4answered.com/questions/view/242e0e3/reactjs-chang...

I would like to change the values ​​with javascript/jquery but when I hit submit, the old initial values ​​are used and not my new ones.
I cannot change the react code, I can only change the values ​​with javascript/jquery as shown below.
var value = "randomIntValue";
$("input[data-reactid='.0.2.0.0.0.0.0.2.0.0.3.0.3.0.0']").val(value).change();

The above code doesn't work!

How to change?
React keeps working with the old value, not from val()
UPD:
here is the solution
const changeValue = (element, value) => {
  const event = new Event('input', { bubbles: true })
  element.value = value
  element.dispatchEvent(event)
}
var someElement = document.querySelector(".text-input");
changeValue(someElement, 200)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2017-06-15
@LiguidCool

1) change exactly the same as not in react.

var a = document.getElementById("name");
  a.value = "some value";

2) if you change the value of the field, and the react uses the old one, then you need to remember that in the react all parameters are set through props and state.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question