Search code examples
htmlinputreturn-valuehtml-select

How to reset a input number back to 0 when a new option is selected?


I want to change/reset the input field back to zero when a new option is selected. I can set the value back to 0 but I want to have that displayed in the input field.

function inputField() {

  var weight = document.getElementById("weight").value;
  weight = 0;

  console.log(weight);
}
<form>
  <select id="pickOne" onChange="inputField()">
    <option id="optionOne" value="1">1</option>
    <option id="optionTwo" value="2">2</option>
  </select>
</form>

<input type="number" id="weight">


Solution

  • Try this:

    function inputField() {
      var weight = document.getElementById("weight");
      weight.value = 0;
    }
    <form>
      <select id="pickOne" onChange="inputField()">
        <option id="optionOne" value="1">1</option>
        <option id="optionTwo" value="2">2</option>
      </select>
    </form>
    
    <input type="number" id="weight">