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 8405665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:53:58+00:00 2026-06-09T22:53:58+00:00

I have several text fields <input class=add type=text> <input class=add type=text> <input class=add type=text>

  • 0

I have several text fields

<input class="add" type="text">
<input class="add" type="text">
<input class="add" type="text">
<input class="add" type="text">
<p id="total"></p>

The values in each field are added dynamically by a PHP script when the page is loaded but the user has the option to edit them. I have been trying to figure out a way to add up all the values using javascript and output them into the #total p tag. I have found a few scripts arround and tried writing one myself but it didnt seam to work.

The best I found looked like this….

function addAll() {
  var sum = ""

  var values = $('.add').each(function(){
    sum += isNaN(this.value) || $.trim(this.value) === '' ? 0 : parseFloat(this.value);
  });

  $('#total').text(sum);
}
$('.add').keyup(displayTotal);

But that didn’t output anything

Am I doing anything seriously wrong?

Cheers

  • 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-09T22:54:00+00:00Added an answer on June 9, 2026 at 10:54 pm

    If it’s numbers you’re adding then this should work:

    function addAll() {
      var sum = 0;
      $('.add').each(function(){
        sum += isNaN(this.value) || $.trim(this.value) === '' ? 0 : parseFloat(this.value);
      });
    
      $('#total').html(sum);
    }
    $('.add').keyup(addAll);  
    

    DEMO

    If you want this to work on page load then add a call to addAll in the .ready() function. Here’s the full code:

    $(document).ready(function(){
    
      addAll();
      $('.add').keyup(addAll);  
    })
    
    function addAll() {
      var sum = 0;
      $('.add').each(function(){
        sum += isNaN(this.value) || $.trim(this.value) === '' ? 0 : parseFloat(this.value);
      });
    
      $('#total').html(sum);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several text fields that are dynamically populated based on user choice from
I have a page with several Ajax.InPlaceEditor fields. Some are single line (text input
I have several text input fields in my page, and few radio buttons. Now
I have one form with several text fields and one button. When i enter
Hi I have several text input boxes, and i have a clear button, they
I am stumped by the following code: <b><i>First name *</b></i> : <input type=text name='<?php
I'm working on a mobile phone web app and I have several text fields
Quick question about CI. I have a view with a form, several text input
I have several applications within my domain that accept similar inputs in text fields.
I have several areas where I use AJAX to submit text fields. var name

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.