Francis Vargas Francis Vargas - 2 months ago 7
HTML Question

Drop down choosing whether Single or Double pay

I hope someone will help me with this, here's the problem. All I want is when choose single in the drop down, I want the numbers in that textbox to be multiply by 1 and whether I choose double in the drop down it will only multiply by 2. Thank you in advance for the help. :)



Also here's my code, I think we can use onchange in here. But I don't know how? Thank you again for the help.

<html>
<head>
<title>Sample</title>
</head>
<body>
<form>
Number:
<input type="text" id="fnum"/>
<br/><br/>
Type:
<select>
<option>Choose</option>
<option value="Single">Single</option>
<option value="Double">Double</option>
</select>
<br/><br/>
Result:
<input type="text" id="result" readonly/>
</form>
</body>
</html>

Answer

Go ahead and try this.
The variable value will contain the value that you have in your select (dropdown)

<html>
<head>
<title>Sample</title>
<script src="http://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha256-/SIrNqv8h6QGKDuNoLGA4iret+kyesCkHGzVUUV0shc=" crossorigin="anonymous"></script>
</head>
<body>
<form>
Number:
<input type="text" id="fnum"/>
<br/><br/>
Type:
<select>
<option>Choose</option>
<option value="Single">Single</option>
<option value="Double">Double</option>
</select>
<br/><br/>
Result:
<input type="text" id="result" readonly/>
</form>
<script>
$('document').ready(function(){
$('select').change(function(){
    var value = $('select').val();
    var fnum = $('#fnum').val();
    if(value == 'Single'){
       var result = fnum*1;
       $("#result").val(result);
    }
    else if(value == 'Double'){
       var result = fnum*2;
       $("#result").val(result);
    }
})
})
</script>
</body>
</html>
Comments