1

When the user enters value in feet, the application should calculate the meter equivalent and display the same in meter textfield.

Formula : 1ft = 0.3048m

When the user enters a value in the meter, the application should calculate the feet equivalent and display the same in feet text field.

Formula : 1m = 3.2808ft

Need to update both values simultaneously when I enter either of the input fields.

sample output

Here is my code:

function LengthConverter(val) {
  var input2 = document.getElementById("meter").innerHTML = val / 3.2808;
  console.log(input2);
  val.value = input2.value;
}
<div class="container">
  <div class='ftm'>
    <label for="feet">Feet:</label><br>
    <input id="feet" type="number" placeholder="Feet" onchange="LengthConverter(this.value)">
  </div>
  <div class='ftm'>
    <label for="meter">Meter:</label><br>
    <input id="meter" type="number" placeholder="Meters">
  </div>
</div>
DBS
  • 9,110
  • 4
  • 35
  • 53

2 Answers2

3

You can use element.addEventListener to watch for changes to the elements, then run a function to update the other one

const feetInput = document.getElementById('feet');
const meterInput = document.getElementById('meter');

function updateMeter() {
  meterInput.value = feetInput.value / 3.2808;
}

function updateFeet() {
  feetInput.value = meterInput.value * 3.2808;
}

// an input event is similar to change but only fires when the actual
// contents of the input field change
feetInput.addEventListener('input', updateMeter);
meterInput.addEventListener('input', updateFeet);
<div class="container">
  <div class='ftm'>
    <label for="feet">Feet:</label><br>
    <input id="feet" type="number" placeholder="Feet">
  </div>
  <div class='ftm'>
    <label for="meter">Meter:</label><br>
    <input id="meter" type="number" placeholder="Meters">
  </div>
</div>
pilchard
  • 12,414
  • 5
  • 11
  • 23
Scrapper142
  • 566
  • 1
  • 3
  • 12
  • Thanks for your answer. can you show what to change in HTML when I use this addEventListener? – Bluefin zen Nov 11 '21 at 17:41
  • You can mention that `input` results in immediate update since it doesn't wait for the element to lose focus to trigger as `change` does. – pilchard Nov 11 '21 at 18:17
1

Try following :

function LengthConverter(val) {
  var input2 = document.getElementById("meter").value = val / 3.2808;
  console.log(input2);
  val.value = input2.value;
}
<div class="container">
  <div class='ftm'>
    <label for="feet">Feet:</label><br>
    <input id="feet" type="number" placeholder="Feet" onchange="LengthConverter(this.value)">
  </div>
  <div class='ftm'>
    <label for="meter">Meter:</label><br>
    <input id="meter" type="number" placeholder="Meters">
  </div>
</div>
  • Thanks, Shreekesh, I got it and I implemented one more function for converting meters to feet. When you commented for the first time I guess it should be working that time it's my mistake that I put an input focus on the text box so that when I type a number in feet it doesn't show in the meters because I haven't focused it yet :) – Bluefin zen Nov 11 '21 at 18:01