Karim Kabbara <kk******@bu.edu> writes:
Oh yes, I forgot to add, I should use the focus function if the num is
not valid. So when they change focus and it does not satisfy the
condition, it place's them back on the field to change.
This is a dangerous thing to do. It traps people in the field. Good
thing it isn't the onblur handler, since this way, they can get away
from the input field by trying to leave it twice (the second time,
there is have been no change, so the onchange handler doesn't
trigger). Don't change it to onblur!
You can collect all the values each time you want to update the result,
or you can cache them. Here I cache them in an object instead of in an
array, together with their sum.
---
var values = {raps:0,lrs:0,is:0,total:0};
function checkAndAdd(elem) {
if (!/^[0-5](\.[05])?$/.test(elem.value)) {
alert("Input illegal: "+elem.value);
setTimeout(function(){elem.focus();},20); // [1]
return;
}
var value = +elem.value;
values.total += value - values[elem.name];
values[elem.name] = value;
elem.form.elements['SumTotal1'].value = values.total;
}
---
Then you can call this function from each element:
---
<input type="text" name="raps" value="0" onchange="checkAndAdd(this)">
<input type="text" name="lrs" value="0" onchange="checkAndAdd(this)">
<input type="text" name="is" value="0" onchange="checkAndAdd(this)">
---
[1] The delay is for browsers which change the focus to the next
element *after* executing the onchange event handler, overriding
the focus set in the handler.
Hope this works :)
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'