SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2004
    Location
    MO
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with Adding Variables

    I'm trying to calculate the Subtotal of several values located in text boxes in a form "securemail". The text boxes [17 of them] are named 1Total, 2Total, 3Total, 4Total, etc. Here's the code:

    function UpdateSubTotal() {
    var SubTotal = 0;
    var k;
    for (i = 1; i <= 17; i++) {
    k = document.securemail.elements[i + "Total"].value;
    SubTotal = SubTotal + k;
    }
    document.securemail.elements["Subtotal"].value = SubTotal;
    }

    The problem I've encountered is this: if, for example, all of the text boxes have a value of zero, the SubTotal would come out 00000000000000000. I'm assuming that this is caused because it's treating them as strings instead of integers. How do I solve this problem? I've never read a JavaScript book or tutorial - I've learned it all through code, so I've missed out on a bunch of fundamental things, such as this.

    Thanks in advance for any and all help.

    -Tony

  2. #2
    SitePoint Member solutionsunited's Avatar
    Join Date
    Jan 2004
    Location
    Singapore
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi tony!

    you can use INTEGER functions / DOUBLE functions to convert the strings value into numeric value in JavaScript.

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="javascript">
    
    function update(oText)
    {
    	if (!/^\d*$/.test(oText.value)) //non-digits
    	{
    		oText.value = oText.value.replace(/[\D]/g, '');
    		return false;
    	}
    	var subtotal = 0, el, i = 1, oForm = oText.form;
    	while (el = oForm.elements['total' + (i++)])
    	{
    		if (!/^\d*$/.test(el.value))
    			el.value = '';
    		subtotal += Number(el.value) || 0;
    	}
    	oForm.elements['subtotal'].value = subtotal;
    }
    
    </script>
    </head>
    <body>
    <h4>Please enter integers only.</h4>
    <form name="securemail" style="font:bold 11px verdana;" onreset="total1.focus()">
    <input id="total1" type="text" name="total1" value="" size="5" onkeyup="return update(this)" />__1<br />
    <input id="total2" type="text" name="total2" value="" size="5" onkeyup="return update(this)" />__2<br />
    <input id="total3" type="text" name="total3" value="" size="5" onkeyup="return update(this)" />__3<br />
    <input id="total4" type="text" name="total4" value="" size="5" onkeyup="return update(this)" />__4<br />
    <input id="total5" type="text" name="total5" value="" size="5" onkeyup="return update(this)" />__5<br />
    <input id="total6" type="text" name="total6" value="" size="5" onkeyup="return update(this)" />__6<br />
    <input id="total7" type="text" name="total7" value="" size="5" onkeyup="return update(this)" />__7<br />
    <input id="total8" type="text" name="total8" value="" size="5" onkeyup="return update(this)" />__8<br />
    <input id="total9" type="text" name="total9" value="" size="5" onkeyup="return update(this)" />__9<br />
    <input id="total10" type="text" name="total10" value="" size="5" onkeyup="return update(this)" />__10<br />
    <input id="total11" type="text" name="total11" value="" size="5" onkeyup="return update(this)" />__11<br />
    <input id="total12" type="text" name="total12" value="" size="5" onkeyup="return update(this)" />__12<br />
    <input id="total13" type="text" name="total13" value="" size="5" onkeyup="return update(this)" />__13<br />
    <input id="total14" type="text" name="total14" value="" size="5" onkeyup="return update(this)" />__14<br />
    <input id="total15" type="text" name="total15" value="" size="5" onkeyup="return update(this)" />__15<br />
    <input id="total16" type="text" name="total16" value="" size="5" onkeyup="return update(this)" />__16<br />
    <input id="total17" type="text" name="total17" value="" size="5" onkeyup="return update(this)" />__17<br /><br />
    <input id="subtotal" type="text" name="subtotal" value="" size="8" onkeyup="return update(this)" />____subtotal
    <br /><br /><input type="reset" />
    </form>
    </body>
    </html>
    A little validation never hurts. Probably best not to start the name of any browser object with a number, just in case. I assumed you wanted integers-only here.
    ::: certified wild guess :::

  4. #4
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    String(i+"Total") might work as you have it, but it would be better using "Total"+i as suggested above, but reading as string, String("Total"+i)
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •