-1
I have 3 fields (a,b,c) that I need to add and play the value in the field (d) as it is being typed, but I can’t do without clicking or typing anything else after the last number.
I tried to use the functions onkeyup
and onkeydown
Is there any way?
function id(el) {
return document.getElementById(el);
}
function calcular(el) {
var a = id('a').value;
var b = id('b').value;
var c = id('c').value;
id('d').value = parseInt(a) + parseInt(b) + parseInt(c);
}
<input type="text" id="a" name="a" onkeyup="calcular()" onkeydown="calcular()" />
<input type="text" id="b" name="b" onkeyup="calcular()" onkeydown="calcular()" />
<input type="text" id="c" name="c" onkeyup="calcular()" onkeydown="calcular()" />
<input type="text" id="d" name="d" disabled="disabled" />
One option would be to start all fields with 0 as value; another would be to validate if the attribute
value
is set and when not set 0.– Woss
gives a look at this function https://api.jquery.com/blur/
– Alisson Almeida
Anderson, starting as 0 worked perfectly, however I was asked to put a checkbox now in the code, that when it is ticado, sum only the first two values (a,b), otherwise sum all (a,b,c). I’m thinking of a way to check the checkbox too now.
– Carmona007