Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert the value of input to another inputs in JS?

Why can't I insert the value of an input into another input? The following example doesn't work:

document.getElementById("input").oninput = () => {
  const input = document.getElementById('input');
  const output = document.getElementById('output');

  // Trying to insert text into 'output'.
  output.innerText = input.value;
};
<input id="input" placeholder="enter value of temperature" />
<br>
<input id="output" />

Thank you!

like image 378
Елисей Горьков Avatar asked Jan 01 '23 20:01

Елисей Горьков


2 Answers

You should use .value instead of .innerText to set the value to an input element, like:

output.value = input.value;

document.getElementById("input").oninput = () => {
  const input = document.getElementById('input');
  const output = document.getElementById('output');

  output.value = input.value;
};
<input id="input" placeholder="enter value of temperature" />
<br>
<input id="output" />
like image 193
Zakaria Acharki Avatar answered Jan 04 '23 09:01

Zakaria Acharki


may be this will be helpful. as per my knowledge. your code will not work on IE. because arrow functions are not supported in IE. however error in your code is "value1.innerText" which is not a right property. because in your code you can see.

value1.innerText=currentValue.value

so if you are fetching value using 'value' property of input. you have to assign a same property for another input box.

so function will be something like this.

var convertTemperature = function convertTemperature() {
      var currentValue = document.getElementById("currentValue");
      var value1 = document.getElementById("value1");
      value1.value = currentValue.value;
};
like image 42
Negi Rox Avatar answered Jan 04 '23 10:01

Negi Rox