SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru hifigrafix's Avatar
    Join Date
    Oct 2005
    Location
    Nashville, TN
    Posts
    628
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [jQuery] Get Sum Of Inputs With Class X

    I've got a form and am using jquery to create row total for about 5 rows. This allows the user to input a qty and part number and then fills out the rest of the information (product name, msrp, dealer cost).

    If all of the row total (qty * price) have a class of say class="rowTotal" is there a quick and easy way of getting the sum of all these classes?

    Thanks

  2. #2
    SitePoint Guru hifigrafix's Avatar
    Join Date
    Oct 2005
    Location
    Nashville, TN
    Posts
    628
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This worked great.

    HTML Code:
    function calculateSum() {
    			var sum = 0;
    			//iterate through each textboxes and add the values
    			$(".row_total").each(function() {
    				//add only if the value is number
    				if(!isNaN(this.value) && this.value.length!=0) {
    					sum += parseFloat(this.value);
    				}
    			});
    			//.toFixed() method will roundoff the final sum to 2 decimal places
    			$("#subtotal").val(sum.toFixed(2));
    			
    		}


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
  •