By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

adding column totals

P: n/a
Can someone tell me what is wrong with this statement?

function subtotal(){
var ttot = 1;
for (var i = 1; i < 7; i++){
if(isPosInt(document.forms[0].total(i).value)){
ttot += document.forms[0].total(i).value;
}
document.forms[0].subtot.value = format(ttot);
}
}

I know the "isPosInt" function works because it works for other things and
Mike wrote it...so I did not add it.
And I do have a global varible elsewhere var e = document.forms[0].elements,
but I also can't get "if(isPosInt(ea['total' + i].value))" to work
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Abby Lee" <ab*******@hotmail.com> wrote in message
news:ui***************************@news.ks.uiuc.ed u...
Can someone tell me what is wrong with this statement?

function subtotal(){
var ttot = 1;
for (var i = 1; i < 7; i++){
if(isPosInt(document.forms[0].total(i).value)){
ttot += document.forms[0].total(i).value;
}
document.forms[0].subtot.value = format(ttot);
}
}

I know the "isPosInt" function works because it works for other things and
Mike wrote it...so I did not add it.
And I do have a global varible elsewhere var e = document.forms[0].elements, but I also can't get "if(isPosInt(ea['total' + i].value))" to work

Will this help? Watch for word-wrap.

<html>
<head>
<title>subtotal.htm</title>
<script type="text/javascript">
function subtotal() {
var ttot = 0;
for (var i=0; i<7; i++) {
var temp = document.forms[0].total(i).value;
if (isPosInt(temp)) {
ttot += parseInt(temp,10);
}
}
document.forms[0].subtot.value = format(ttot);
}
function isPosInt() {
// your code here!
return true;
}
function format(what) {
// your code here!
return what;
}
</script>
<style type="text/css">
..numb { text-align:right }
</style>
</head>
<body>
<form>
<br><input type="text" name="total" size="5" value="1" class="numb">
<br><input type="text" name="total" size="5" value="2" class="numb">
<br><input type="text" name="total" size="5" value="3" class="numb">
<br><input type="text" name="total" size="5" value="4" class="numb">
<br><input type="text" name="total" size="5" value="5" class="numb">
<br><input type="text" name="total" size="5" value="6" class="numb">
<br><input type="text" name="total" size="5" value="7" class="numb">
<br>=======
<br><input type="text" name="subtot" size="5" value="" class="numb">
<br><input type="button" value="Total!" onclick="subtotal()">
</form>
</body>
</html>

Obviously you should remove the values for all "total" fields.
Jul 23 '05 #2

P: n/a
On Wed, 22 Sep 2004 12:18:36 -0500, Abby Lee <ab*******@hotmail.com> wrote:
Can someone tell me what is wrong with this statement?

function subtotal(){
var ttot = 1;
for (var i = 1; i < 7; i++){
if(isPosInt(document.forms[0].total(i).value)){
Don't use parentheses for subscripting. Not only is it wrong, but only IE
will accept it.
ttot += document.forms[0].total(i).value;
This will act as string concatenation. Use:

ttot += (+document.forms[0].total[i].value);

The unary plus will force the value to a number.

I also suspect that 'total[i]' is incorrect. If the form control is named,
total<N>, where <N> is number, see later in this post. My suggestion will
apply here, too.

[snip]
And I do have a global varible elsewhere var e =
document.forms[0].elements,
It should probably be local.
but I also can't get "if(isPosInt(ea['total' + i].value))" to work


How is 'ea' set? In the previous thread, you used

var ea = document.forms[0].elements['elementName'];

What you need is:

var ea = document.forms[0].elements;

which you already have as the value of the variable, e.

If 'i' in the expression

ea['total' + i]

was 1, it would look up the control, 'total1' in the first form.

I probably didn't point you to the FAQ notes on accessing form controls. I
should have. The link below covers the basics. The resource linked at the
end of section goes into more detail.

<URL:http://jibbering.com/faq/#FAQ4_13>

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3

P: n/a
> function subtotal() {
var ttot = 0;
for (var i=0; i<7; i++) {
var temp = document.forms[0].total(i).value;
if (isPosInt(temp)) {
ttot += parseInt(temp,10);
}
}
document.forms[0].subtot.value = format(ttot);
}


The code did not work fo me.
If I put an alert() message before the "var temp=document.forms[0].total
(i).value", I will see it when it trigger the code. But if I put an alert
after I will not see it.
Jul 23 '05 #4

P: n/a
"Abby Lee" <ab*******@hotmail.com> wrote in message
news:ui***************************@news.ks.uiuc.ed u...
function subtotal() {
var ttot = 0;
for (var i=0; i<7; i++) {
var temp = document.forms[0].total(i).value;
if (isPosInt(temp)) {
ttot += parseInt(temp,10);
}
}
document.forms[0].subtot.value = format(ttot);
}


The code did not work fo me.
If I put an alert() message before the "var temp=document.forms[0].total
(i).value", I will see it when it trigger the code. But if I put an alert
after I will not see it.


What's your browser?

I only tested it under IE5.5.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.