1
How to pass the result of this script to a field and already returning formatted in Real Currency
</head>
<body>
<input type="checkbox" id="iv"/></br>
<input type="text" id="campo" value="10"></br>
<select name="children-qnt" id="children-qnt">
<option value="0">0</option>
<option selected value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Result: <span id="resultado">10</span>
<script type="text/javascript">
var $campo = byId('campo'),
$select= byId('children-qnt'),
$checkbox = byId('iv');
function byId(element){
return document.getElementById(element);
}
function updateResult(){
var result = parseInt($campo.value) *
parseInt($select.value);
byId('resultado').innerHTML = $checkbox.checked ? result * 2 : result;
}
$campo.addEventListener('keyup', updateResult);
$select.addEventListener('change', updateResult);
$checkbox.addEventListener('change', updateResult);
</script>
Ok this was just what I needed more not this adding up the last decimal places and when I put id in the field does not appear for anything exe: <input type="text id="result" />
– Fabio Henrique
You cannot and should not put two identical id’s on the same page, and the id="result" you already have and as your script is using it, can and will give conflict! About the fact that it is not adding up the decimals, this happens by the command
parseInt
, 'Cause you’re telling him to take only the integers of value, which means if you have 10.50 he’ll take the whole 10!– Ademílson F. Tonato