Javascript length converter

[img] http://i.imgur.com/ch761kR.png [/ img] I have to reproduce the length converter as shown in the picture above. When you put the number in the field and press the button, it gives the result to the right of the button you selected. I started by creating buttons. Here is the code, my problem is that I do not know how to create a function that returns the result of the selected button outside the field.

Valeur:<input type="text" name="valeur" value="0" oninput="LengthConverter(this.value)"onchange="LengthConverter(this.value)"> Resultat ici<br>
<button type="button" id="inchToCm" onclick="pvc()">Pouces vers cm</button>
<button type="button" id="cmtoInch">CM vers pouces</button>
<button type="button"id="celciusToFarenheit">Celcius vers
Farenheit</button>
    <button type="button" id="farenheitToCelcius">Farenheit vers Celcius</button>

I started making a function to convert lengths:

<script>
function pvc(valnum){
    document.getElementById("outputCm").innerHTML=valnum/0.39370;
}

</script>

Thank you in advance

+4
source share
1 answer

I added a p element for "Resultat ici" with an identifier so that the value can be obtained from the user.

Valeur:<input type="text" id="value" name="valeur" value="0" /> 

<p id="result">Resultat ici</p>

<button type="button" id="inchToCm" onclick="pvc()">Pouces vers cm</button>
<button type="button" id="cmtoInch" onclick="pvc()">CM vers pouces</button>
<button type="button"id="celciusToFarenheit" onclick="pvc()">Celcius vers
Farenheit</button>
<button type="button" id="farenheitToCelcius" onclick="pvc()">Farenheit vers 
Celcius</button>

:

<script>
     function pvc() {  
        //get user input
      var input = document.getElementById("value").value;

     /*implement if statement tests here for the next 3 buttons
      (cmtoinch, celctofahr, fahrtocelc)
      */

      //result of the calculation that is then put into the result p element with the id of result.
      var result = document.getElementById("result");
      result.innerHTML = input / 0.39370;

    }
</script>

, : https://codepen.io/capozzic1/pen/MmeXxR

: codepen if

+2

Source: https://habr.com/ru/post/1675428/


All Articles