I am new in JavaScript and can’t figure out why is it not working.
HTML :
JavaScript
x
9
1
<input type="text" id="weight_value" placeholder="Input Weight" />
2
<select id="weight_unit">
3
<option value="lbs">KG to Pound</option>
4
<option value="kg">Pound to KG</option>
5
</select>
6
<button title="Convert" onclick="display_result()">Convert</button>
7
<div id="result"></div>
8
<script src="hero.js"></script>
9
JavaScript:
JavaScript
1
12
12
1
function display_result(){
2
3
var kg = 0.45359237
4
var results = ""
5
6
if ($(this).val() == "kg") {
7
$("#result").val( $("#weight_value").val()*kg)
8
} else {
9
$("#result").val( $("#weight_value").val()/kg)
10
}
11
};
12
Advertisement
Answer
Try this.
JavaScript
1
11
11
1
function display_result(){
2
var kg = 0.45359237
3
var results = ""
4
if ($('#weight_unit').val() == "kg") {
5
$("#result").html( $("#weight_value").val()*kg)
6
}
7
else {
8
$("#result").html( $("#weight_value").val()/kg)
9
}
10
};
11