Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8304983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:58:43+00:00 2026-06-08T17:58:43+00:00

I’m facing a little problem with my jQuery / Javascript code. I have several

  • 0

I’m facing a little problem with my jQuery / Javascript code.

I have several values on a page, like :

checkbox EXAMPLE EXAMPLE 150
checkbox EXAMPLE EXAMPLE 300
checkbox EXAMPLE EXAMPLE 300

I have this little code, which basically just add the numerical values (150, 300, 300) to an array, and then, sum the array in the “total” variable. (Well, at least this is what I’m trying to do…)

Here’s the code :

 $('.pack_check').click(function() {
        var cout = [];
        total = 0;
        $('#services_pack input:checked').each(function() {
            cout.push(this.cout);
            $.each(cout,function() {
                total += parseInt(this);
                });
            });
        });
    });

My problem is, that when I click on my first checkbox, it’s ok, total = 150
But, if I click on the second checkbox, total = (150 + 150 + 300) which is a bit annoying…

Anyone ?

Thanks in advance !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:58:44+00:00Added an answer on June 8, 2026 at 5:58 pm

    The problem is likely that you’ve got a nested loop adding to your total. So for every checked checkbox you first add its .cout to the array, and then you add every element in the array to the total again. Also you have too many closing });. Try this:

    $('.pack_check').click(function() {
        var cout = [];
        total = 0;
        $('#services_pack input:checked').each(function() {
            cout.push(this.cout);
        });
        $.each(cout,function() {
            total += parseInt(this, 10);
        });
        // do something with total
    });
    

    Or if you just need the total eliminate the cout array:

    $('.pack_check').click(function() {
        var total = 0;
        $('#services_pack input:checked').each(function() {
            total += parseInt(this.cout, 10);
        });
        // use total here
    });
    

    Note that parseInt() takes a second parameter to specify the radix, so if you want to work in base 10: parseInt(stringToParse,10) – avoids problems if the string starts with a leading 0 or leading 0x, which would otherwise be interpreted as base 8 and base 16 (respectively).

    Having said that, I recommend the unary plus operator to convert a string to a number:

    total += +this;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have two tables with like below codes: Table: Accounts id | username |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.