simple addition

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

simple addition

Post by jauson »

Hi, I want to perform simple addition in jquery.


assumed I have 8 fields, and I want to auto add my field_total.

field1 = 1
field2 = 2
field3 = 3
field4 = 4
field5 = 5
field6 = 6
field7 = 7

field_total = 28

below is my code that i get in any tutorial. how do i add to get this thing done?.



<script>
function newTotal(){
var tot=0;
$('.myAddClass').each(function(){
tot+= 1*$(this).val();
}};
$('#field_total').val( tot);
}

$('.myAddClass').bind('keyup', newTotal);
</script>



cheers!
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: simple addition

Post by Gopesh »

jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Re: simple addition

Post by jauson »

thanks Gopesh!
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Re: simple addition

Post by jauson »

this is mine.
<script type="text/javascript">
function updatesum() {
document.form.field_total.value = (document.form.tax.value -0) + (document.form.sss.value -0) + (document.form.sss_loan.value -0) + (document.form.philhealth.value -0) + (document.form.pagibig.value -0) + (document.form.pagibig_loan.value -0) + (document.form.medicare.value -0);
}
</script>
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: simple addition

Post by Gopesh »

Hi,Did u get the result? Are u using pure javascript or jquery? if u don't get the answer pls show ur html code also?
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Re: simple addition

Post by jauson »

Hi Gopesh!

I'm using the code above of your latest post. I think it isn't a jquery. its javascript. cos when i change the value of my one textfield it isnt change immediately on my txt_total. :)

Regards,
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: simple addition

Post by Gopesh »

Hi,you need to add an onChange event for that. It is better to use jquery ,bcoz it reduces the code and makes it easier.
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Re: simple addition

Post by jauson »

can you give me a quick reference to learn faster in jquery? you have skype?
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Re: simple addition

Post by jauson »

by the way i have onchange for each field i have.

thanks
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: simple addition

Post by Gopesh »

HI,there are a lot of tutorials available in internet for learning jquery.
Good Links:http://blog.themeforest.net/tutorials/j ... eo-series/
E-book:-jQuery: Novice To Ninja
Without showing ur html code it is not possible to find a solution to ur problem.
Post Reply