Solved

jquery forms calculation from array of inputs

Posted on 2014-04-06
4
832 Views
Last Modified: 2014-04-24
Hi

I have a form that can have any number of inputs, they are displayed on the page depending on how many items in this category.. could be 1 or could be 15 or anywhere inbetween.

<input type='text' name='productname[]' />
<input type='text' name='qty[]' />
<input type='text' name='price[]' />

Open in new window




AT the bottom  of the form I have a span to hold the 'total value'

<span class='totalvalue'></span>

Open in new window



when a user enters a figure into the qty textbox, I need it to update the total at the bottom...

how the form looks
Any ideas?

Cheers
0
Comment
Question by:cycledude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 500 total points
ID: 39980985
There are a lot of ways to attack this.  If you are controlling the server side code, I like to make use of the data-attribute properties.  This makes it easier to see what is going on.

You will have to do some front end work to make this pretty.  Here is a live sample to the below code http://jsbin.com/ziwakaku/1/edit?html,js,output

HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
  <meta charset="utf-8">
  <title>padas Q_28406036</title>
</head>
<body>
<div class="product">
  ABC123 £5.00
<input type='text' name='qty' data-product="abc123" data-price='5' />
</div>
  <div class="product">
  XYZ789 £10.00
<input type='text' name='qty' data-product='XYZ789' data-price='10'/>

</div>
  <div class="product">
  MNO356 £3.00
<input type='text' name='qty' data-product='MNO356' data-price='3' />

</div>

  <div class='totalvalue'></div>
</body>
</html>

Open in new window

JQUERY
function calcTotal(product) {

    var price = $('input[data-product="' + product + '"]').attr('data-price');
    var qty = $('div.product input[data-product="' + product + '"]').val();
    if (qty !== '') {
        return price * qty;

    } else {
        return '0';
    }

}



$(function() {
$('input[name="qty"]').change(function () {
    var totalValue = 0;
    $('input[name="qty"]').each(function () {
        var product = $(this).attr('data-product');
        totalValue += parseInt(calcTotal(product), 10);

    });
 
    $('.totalvalue').html('Total £' + totalValue);
});
});  

Open in new window

0
 

Author Comment

by:cycledude
ID: 39989812
thanks, i will give it a try
0
 

Accepted Solution

by:
cycledude earned 0 total points
ID: 40010638
sorry for the delay

heres what I did, which may help others, based on your code, but I just simplified it a little..

	$('input.qty').change(function () {
		var totalValue = 0;
		$('input.qty').each(function () {
			var qty = 0;
			var price = parseFloat($(this).attr('data-price'));
			qty = parseInt($(this).val()) || 0;
			
			totalValue += qty * price;
			
		});
		
		
		$('.totalprice').html('£' + totalValue);
		$('.formtotal').val(totalValue);
	})

Open in new window

0
 

Author Closing Comment

by:cycledude
ID: 40019505
Great stuff thanks for the help ;o)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Customizing jQuery UI Accordion CSS 3 40
jQuery Toggle & Anchor Links 5 55
Javascript: Setting a dropdown based on another dropdown selection 5 40
selected value 16 25
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question