SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "for loop" array

    I was wondering if someone could help me with this. I am collecting numbers from each input with the class name 'totalrow'. That part is fine, but now I want to take all the results from each 'totalrow' and combine it into one final result. The 'totalrow's are generated dynamically through PHP.

    If I put alert(total[i]); after 'total[i] = + document.getElem...' it alerts each of the correct values.

    In a nutshell: I want to get all values from the 'totalrows' classname and add them together to alert the final total.

    Code JavaScript:
    for (i=0;i<document.getElementsByTagName("input").length; i++) {
    		var total = new Array();
    		if(document.getElementsByTagName("input").item(i).className == "totalrow"){
    		total[i] = + document.getElementsByTagName("input").item(i).value;
    			}
    		}

    Any help is appreciated! Thanks!

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    var total=0;
    for (i=0; i<document.getElementsByTagName("input").length; i++) {
        if(document.getElementsByTagName("input").item(i).className == "totalrow"){
            total += document.getElementsByTagName("input").item(i).value;
        }
    }
    Saul

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! It works great!

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try parseFloat()
    Saul


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
  •