4

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!

asynts
  • 2,213
  • 2
  • 21
  • 35
  • 2
    Possible duplicate of [Set Value of Input Using Javascript Function](https://stackoverflow.com/questions/5700471/set-value-of-input-using-javascript-function) – ponury-kostek Sep 14 '18 at 09:16

3 Answers3

9

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" />
asynts
  • 2,213
  • 2
  • 21
  • 35
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • Should we not close it as dupe instead? – Rajesh Sep 14 '18 at 09:18
  • Actually, I'm the downvoter. And its not because your answer is incorrect. Its because, its redundant and hence its not required. As more experienced users, we should look for dupes for such posts and close them instead of answering. – Rajesh Sep 14 '18 at 09:31
  • Thanks for your intervention @asynts (approved) – Zakaria Acharki Sep 14 '18 at 14:13
1

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;
};
Negi Rox
  • 3,828
  • 1
  • 11
  • 18
-1

You can get real time value by below code,

jQuery('input#currentValue').change(function(){
    var current_value = jQuery('input#currentValue').val();
    jQuery('input#value1').val(current_value );
});
ElusiveCoder
  • 1,593
  • 1
  • 8
  • 23