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

  • SEARCH
  • Home
  • 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 8948887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:06:38+00:00 2026-06-15T13:06:38+00:00

I want to alert the total amount calculated but I don’t know why instead

  • 0

I want to alert the total amount calculated but I don’t know why instead of calculating its just appending the values.

The code:

    $('#main button').click(function(){
    id = $(this).attr('id');
    parts = id.split('_');
    item_id = parts[0];
    pairs = $('#'+item_id+'_pairs').val();
    females = $('#'+item_id + '_females').val();
    males = $('#'+item_id + '_males').val();
    if(pairs >0){
        counted_pairs = pairs*2;
    }else{
        counted_pairs = 0;
    }
    total_fishes = (males + females + counted_pairs);
    alert(total_fishes);
});

If I add 1 pair 1 male and 1 female in form it should show 4 total but its showing 111 seems appending not calculating?

  • 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-15T13:06:39+00:00Added an answer on June 15, 2026 at 1:06 pm

    Change total_fishes = (males + females + counted_pairs); to this:

    total_fishes = ((males * 1 + females * 1) * 1 + counted_pairs * 1);
    

    This will work, I know it seems odd but Javascript converts the variables to numbers when u use * 1 + value * 1. I can’t explain why but I saw this peace of code a couple of years ago in cPanel x and I use it a lot.

    Edit:

    There is actually a difference when you are using user input you can check if the value is wrong using isNaN this will not work when you use parseInt because it supresses the error and tries to create an integer anyways.

    TestCode:

    var males = "2,4";
    var females = "3";
    var counted_pairs = "33";
    
    var result = ((males * 1 + females * 1) * 1 + counted_pairs * 1);
    var resultb = (parseInt(males) + parseInt(females * 1) + parseInt(counted_pairs));
    
    if (isNaN(result)) {
     alert("Input is wrong my method");   
    }
    
    if (isNaN(resultb)) {
     alert("Input is wrong using parseint");   
    }​
    

    Result: Only the first alert screen will be shown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On entering rate and quantity I want the total amount to be calculated automatically,
Mobile browsers. Jquery dialog alert box. I don't want to use alert() because it
When you want to alert a user of something once (one time notes about
I have a JavaScript function code where I want to alert. function msgalert(x,y) {
I have a JavaScript function code where I want to alert. function msg(x,y) {
When someone double clicks on the entire row, I want an alert. How do
I want to give an alert after person sign up successfully. And then I
I want to display an alert box multiple times with a button click event
I want to present an alert window with no button on it, that means
I want to set an alert service for my website users for there tasks.

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.