I am having difficult displaying the contents of a variable in a total input field. Basically this code is functioning which is great:
<script>
$(function() {
$('#product-unit-val, #total-unit-sales, #number-reps').change(function() {
var total = parseInt($("#product-unit-val").val()) *
parseInt($("#total-unit-sales").val()) *
parseInt($("#number-reps").val());
if (!isNaN(total)) {
$("#sales-val").html(total);
}
});
});
</script>
Average product unit value <input type="text" id="product-unit-val" /><br/>
Total unit sales value <input type="text" id="total-unit-sales" /><br/>
Number of reps <input type="text" id="number-reps" /><br/>
Total sales value <span id="sales-val"></span>$
However I want to display the total value in an input field like:
<script>
$(function() {
$('#product-unit-val, #total-unit-sales, #number-reps').change(function() {
var total = parseInt($("#product-unit-val").val()) *
parseInt($("#total-unit-sales").val()) *
parseInt($("#number-reps").val());
if (!isNaN(total)) {
$("#sales-val").html(total);
}
});
});
</script>
Average product unit value <input type="text" id="product-unit-val" /><br/>
Total unit sales value <input type="text" id="total-unit-sales" /><br/>
Number of reps <input type="text" id="number-reps" /><br/>
Total sales value <input type ="text" id="sales-val"/>
Am I missing the obvious? I just can’t seem to get this working.
Many Thanks
Use val to change the value of an input :
But be careful with parseInt, always specify the radix :