Rahatul Amin Ananto Rahatul Amin Ananto - 6 months ago 19
HTML Question

KG to lbs conversion not working properly

I am new in JavaScript and can't figure out why is it not working.

HTML :

<input type="text" id="weight_value" placeholder="Input Weight" />
<select id="weight_unit">
<option value="lbs">KG to Pound</option>
<option value="kg">Pound to KG</option>
</select>
<button title="Convert" onclick="display_result()">Convert</button>
<div id="result"></div>
<script src="hero.js"></script>


JavaScript:

function display_result(){

var kg = 0.45359237
var results = ""

if ($(this).val() == "kg") {
$("#result").val( $("#weight_value").val()*kg)
} else {
$("#result").val( $("#weight_value").val()/kg)
}
};

Answer

Try this.

function display_result(){
    var kg = 0.45359237
    var results = ""
    if ($('#weight_unit').val() == "kg") {
        $("#result").html( $("#weight_value").val()*kg)
    } 
    else {
        $("#result").html( $("#weight_value").val()/kg)
    }
};