Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  135] [ 2]  / answers: 1 / hits: 29059  / 12 Years ago, fri, february 1, 2013, 12:00:00

I am kinda new to Jquery / JS and after a little help. I realise there hopefully is a much better way to do this, and any help would be greatly appreciated.



I am building a form that can calculate a total. That is:



'Cost' x '(1 + percent(%))' = 'total cost'



I have it working nicely, though I want to make it so that if I change either the 'percent' field, or the 'cost' field then the total amount updates. At the moment only if you update the 'cost' will the total update.



Hope that makes sense. Code is below.



$(document).ready(function(){

$(#cost).change(function() {
var findprofit = parseFloat($(#cost).val());
var profit = (findprofit*.01)
var margin = parseFloat($(#percentage).val());
var total = profit*margin;
$(.profit_1_1).html(total);
var totalprofit = (total + findprofit);
$(#total).val(totalprofit);
});

<input type=text name=cost id=cost />
<input type=text name=percentage id=cost percentage />
<input type=text name=total id=total readonly />

More From » jquery

 Answers
8
$(#cost, #percent).change(function() { 
...
});


Should do the trick.



Edit: You'll need to give your percent input an id of percent. Two elements may not have the same ID.



<input type=text name=cost id=cost  />
<input type=text name=percentage id=percent percentage />
<input type=text name=total id=total readonly />


Also I'm not sure what that percentage is at the end of the input. Percentage isn't a valid attribute.


[#80485] Wednesday, January 30, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mireyag

Total Points: 73
Total Questions: 107
Total Answers: 85

Location: Ukraine
Member since Sun, Dec 13, 2020
3 Years ago
mireyag questions
Sun, Aug 15, 21, 00:00, 3 Years ago
Wed, Dec 16, 20, 00:00, 3 Years ago
Tue, Sep 1, 20, 00:00, 4 Years ago
Sun, Jul 5, 20, 00:00, 4 Years ago
;