最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to insert the value of input to another inputs in JS? - Stack Overflow

programmeradmin3浏览0评论

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" />

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!

Share Improve this question edited Sep 14, 2018 at 10:55 asynts 2,4342 gold badges27 silver badges37 bronze badges asked Sep 14, 2018 at 9:06 Елисей ГорьковЕлисей Горьков 4476 silver badges12 bronze badges 1
  • 2 Possible duplicate of Set Value of Input Using Javascript Function – ponury-kostek Commented Sep 14, 2018 at 9:16
Add a ment  | 

3 Answers 3

Reset to default 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" />

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;
};

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 );
});
发布评论

评论列表(0)

  1. 暂无评论