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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:45:57+00:00 2026-06-09T15:45:57+00:00

I am trying to do a addition sum with javascript to work out the

  • 0

I am trying to do a addition sum with javascript to work out the user total cost when their goods and shipping are added together, however all I get returned is NaN why is? Here is a fiddle of my code

Code ported from fiddle:

 $("#country").change(function() {
    var shipping_cost = parseFloat($("#shipping").text().replace(pattern, ''));
    var total      = parseFloat($("#goods").text().replace(pattern, ''));
    var pattern = /[^0-9.-]+/g;
    var result = total + shipping_cost;
    console.log(result);

       if($(this).val() == "17") {
           $("#shipping").text("£0.00");
           $("#full_price").text("£"+result);
       } else {
           $("#shipping").text("£5.00");
           $("#full_price").text("£"+result);
       }
 });​

Markup:

<select id="country">
    <option value=17>Option 1</option>
    <option value=18>Option 2</option>
    <option value=19>Option 3</option>
</select>

<p id=goods>£5.00</p>

<p id=shipping>£0.00<p>
  • 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-09T15:45:58+00:00Added an answer on June 9, 2026 at 3:45 pm

    Move var pattern = /[^0-9.-]+/g; up, and everything should work fine:

    var pattern = /[^0-9.-]+/g;
    var shipping_cost = parseFloat($("#shipping").text().replace(pattern, ''));
    var total      = parseFloat($("#goods").text().replace(pattern, ''));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to figure out how many clock cycles or total instructions it
I'm trying to build out a Google News Sitemap in addition to a normal
I am trying to get my iPhone application to work with HTTPS in addition
I am trying to sum the cost for a series of items grouped by
I'm trying to figure out how to have a few static cells in addition
I'm trying to make a simple addition calculator in C to learn how to
I am trying dynamically add children DIV under a DIV with ID=prnt. Addition of
I'm trying to speed up a large RSpec project's tests. In addition to using
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Trying to figure out how I can do this properly. The print_r looks like

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.