Forum Moderators: open
I have multiple text fields like this:
<input type="text" id='updateA1' name="updateA1" size="2" value="0" onkeyup="updateA();" /> and a function like so (trimmed it a bit):
function updateA() {
var totalA =
parseFloat(updateA1.value) +
parseFloat(updateA2.value);
....
};
so basically whenever a number is typed in the text fields, it calculates a total and outputs it to the page. Like I said, it works fine except in FireFox.
What do I need to do to make the onkeyup work in FireFox? It is never triggered.
Thanks!
<html>
<head>
<title>Test</title>
</head>
<body>
<input type="text" id='updateA1' name="updateA1" size="2" value="0" onkeyup="updateA();" />
<script type="text/javascript">
function updateA() {
var u = document.getElementById('updateA1');
alert(u.value);
}
</script>
</body>
</html>
I get the input this way: var totalA = parseFloat(updateA1.value)
You get the input this way: var u = document.getElementById('updateA1')
is there any way to shorten that code, or do I need to declare a variable for each input?
thanks!
Also, if you're going to be doing numeric calculations on the value, you'll probably want to perform some validation on the items anyway, so getting them into a variable is a good thing. It's considered good practice to define your variables at the beginning of the function. If you don't care about validation you could still do something like this:
function updateA() {
var updateA1 = document.getElementById('updateA1'),
updateA2 = document.getElementById('updateA2'),
totalA = parseFloat(updateA1.value) + parseFloat(updateA2.value);
//...
};