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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:21:30+00:00 2026-06-01T12:21:30+00:00

For some reason i can’t get the variable ‘total’ to define at all… I

  • 0

For some reason i can’t get the variable ‘total’ to define at all…

I defined it on like 74 but it does’t want to stick for some reason.. what am i doing wrong? Thanks in advance!

$(document).ready(function() {

    function getParameterByName(name)
    {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.search);
        if(results == null)
            return "";
        else
            return decodeURIComponent(results[1].replace(/\+/g, " "));
    }

    $(".tab-content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab-content:first").show(); //Show first tab content


    $('.question-form-submit').click(function(e) {
        e.preventDefault();
        var activeTab = '#'+$(this).attr('name');
        var activeClass = activeTab.substr(5);
        $("ul.tabs li").removeClass("active"); 
        $('ul li:nth-child('+activeClass+')').addClass("active"); //Add "active" class to selected tab
        $(".tab-content").hide(); //Hide all tab content
        $(activeTab).fadeIn(); //Fade in the active ID content


        $('.meter-value').removeClass('meter-width');

        switch (activeClass) {
            case '2' :
                $('.meter-value').attr('style', 'background-color: #9496c9; width: 46.5%;');
                break;

            case '3' :
                $('.meter-value').attr('style', 'background-color: #9496c9; width: 67%;');
                break;

            case '4' :
                $('.meter-value').attr('style', 'background-color: #9496c9; width: 100%;');
                break;

        }

        return false;
    });

    $('.quantity, .init_cost').change(function() {

        var row_id = $(this).attr('id');
        var row_number = row_id.substr(9);
        var item_cost = $('#cost_'+row_number).attr('value');
        var item_quantity = $('#quantity_'+row_number).attr('value');
        var final_cost = item_cost * item_quantity;

        $('#final_cost_'+row_number).val(final_cost).formatCurrency();;

    });

    $('.row input').each(function(index) {

        var row_id = $(this).attr('id');
        var row_number = row_id.substr(9);
        var item_cost = $('#cost_'+row_number).attr('value');
        var item_quantity = $('#quantity_'+row_number).attr('value');
        var final_cost = item_cost * item_quantity;

        $('#final_cost_'+row_number).val(final_cost).formatCurrency();;

    });

        var total = 0;
        $('.final_cost').each(function(index) {

            var final_cost = $(this).attr('value').substr(1);
            var total = total + final_cost;
            console.log(total);

        })

});
  • 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-01T12:21:31+00:00Added an answer on June 1, 2026 at 12:21 pm

    The total in the each function is shadowing the outer one.

    A simpler example of the same thing is here:

    (function()
     {
         var total = 1;
         console.log("total 1: " + total);
         (function()
          {
              console.log("total 2: " + total);
              var total = total + 3;
              console.log("total 3: " + total);
          })()
     })();
    

    In addition to the shadowing, you have to consider hoisting. Because the inner var is hoisted to the top, the inner function is roughly equivalent to:

    function()
    {
        var total = undefined;
        console.log("total 2: " + total);
        total = total + 3;
        console.log("total 3: " + total);
    }
    

    In this case, I think you simply don’t want the inner var keyword. In other cases, you would use a different variable name.

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

Sidebar

Related Questions

I know PHP 5 already supports SQLite but for some reason I can't get
This should be pretty simple but for some reason I can't get this value
For some reason I can't get this to work at all. I have read
For some reason I can't get this script to run when I add the
I'm fairly new to JQuery and for some reason I can't get the currently
This is probably very easy to do but for some reason I can't seem
I am trying to initialize directX, but for some reason it can't create d3d
An easy problem, but for some reason I just can't figure this out today.
for some reason I can't get my head around mustache templating and merging two
for some reason I can't get my tableview to enter editing mode. It's a

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.