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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:46:25+00:00 2026-06-04T05:46:25+00:00

I have this script $(‘table#preview td.price’).each(function(){ var price = parseInt($(this).html()); var total; if(price ==

  • 0

I have this script

$('table#preview td.price').each(function(){
var price = parseInt($(this).html());
var total;
if(price == ''){
price = 0;
}
total += price;
alert(total);
});

What it does is get the column that has the class price then supposedly adds it all up.

However, all I get from this code is NaN. I don’t get what’s wrong with the code.

Please note the script if(price == ”). I’ve done this because initially there are no contents in the table.

Edit: Here the html

    <table>
    <tr>
    <th>Name</th>
    <th>Price</th>
    </tr>
    <tr>
    <td>pen</td>
    <td class="price>6</td>
    <td>paper</td>
    <td class="price>8</td>    
    </tr>
    </table>
  • 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-04T05:46:26+00:00Added an answer on June 4, 2026 at 5:46 am

    Try using the .text() method instead of .html() method, it should hopefully help get rid of the NaN errors. You’ll want to declare the total outside the scope of each iteration so it doesn’t get reset each time. Try giving this a go, I’ve simplified it slightly to also include a check against the number price:

    var total = 0;
    $('table#preview td.price').each(function()
    {
        var price = parseInt($(this).text());
        if (!isNaN(price))
        {
            total += price;
        }
    });
    
    alert('The total is: ' + total);
    

    Update

    Here’s a jsFiddle. N.B. .html() should also work.

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

Sidebar

Related Questions

I have this script: $.each($(this)[0].files, function(i, file){ if (input.files && input.files[0]) { var reader
I have this script loaded on a page: (function() { window.alert('bookmarklet started'); function AjaxSuccess(data,
I have this script: $(#menu ul li).mouseover( function () { $(this).find(.submenu).fadeIn(slow); } ); var
I have this script: function slideSwitch() { var ae = jQuery('#featured-right a.active'); if (
I have this script which is used to append: function append_place(type) { var shop_type
I have this script, running on a links list: $('li#linkcat-25 a').bind('click', function (e) {
I have this script: $(document).ready(function() { $('#Checkout1').addClass('disabled'); $('#voorwaarden').change(function() { $('#Checkout1').attr('disabled', $(this).is(':checked') ? null :
I have this script protovis : <script type=text/javascript+protovis> var w = 600; var h
I have this script for moving a character in a game in a html
I have this script using jquery: $(document).ready(function() { $('.fotky,.video,.diskusia,.onas,.zbrane,.akcie,.tabroz,.tabburk,.tabhil,.tablest').append('<span class=hover></span>').each(function () { var $span

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.