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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:15:22+00:00 2026-06-01T02:15:22+00:00

ive a function that does calculations on a table using jquery when any input

  • 0

ive a function that does calculations on a table using jquery when any input field is updated. It works fine but i need to be able to call it update a specific row when i load in data. I cant seem to get it working properly either it updates when the an input field changes and it wont work when i call it to update a specific row.

Im trying to pass a parameter into the function to tell what row to update when i need it and when it detects a change event it checks if the variable was passed in. I was trying to check if the passed in variable was undefined or null but i cant seem to get to work. What ami doing wrong?

my code;

so a call to update a certain row;

////////////////////////////////////////////
//load prices and pid from range selected
////////////////////////////////////////////

$(document).ready(function(){
    $("#range_select").change(function(event){  

    //get the range id
    $id=$("#range_select").val();

    var i;
    var loadedValues;
    var result;
    var pid;

    loadedValues=0;

    //clear All the prices if loaded, reset background color
    $(".price").val(0);
    $(".price").css("background-color","#FFF"); 

    //clear ALL product id
    $(".productid").val(0);
    ///////////////////////////////////////////////////////////

    //note the url will break if site changed
    $.ajax({url:"/mysite/products/list_products_from_range/"+$id, success:function(result){     

        /*if(result.length==0){
            //no results from ajax request
            alert('No products found for this range.);
            return false;
        }*/

        //parse the returned JSON object,for each parse of result we check the table
        $.each(jQuery.parseJSON(result), function() {       

            //console.log("product id="+this['Product']['id']); 

            pid=this['Product']['id'];
            var price=this['Product']['price'];
            var height=this['Product']['height'];
            var width=this['Product']['width'];

            /*console.log("price="+price);
            console.log("h="+height);
            console.log("w="+width);*/


            /////////////////////////////////////////////////////////////
            //now we have to go through the table and insert the values     

            i=-1;

            var rows = $("#productentry tr:gt(0)"); // skip the header row              
            rows.each(function(index) {     

                i++;
                var h = $("td:eq(3) .h", this).val();
                var w = $("td:eq(4) .w", this).val();

                //console.log(h +'x'+w);                
                //console.log("if "+w+" = "+width+" and "+h+" = "+height);              
                //console.log('index='+index);


                if(w==width && h==height){
                    //increment count of loaded values
                    loadedValues++;

                    //set the price
                    $("#listprice_"+i).val(price);

                    //set the pid
                    //alert(pid);
                    $("#productid_"+i).val(pid);

                    //change price textbox to visually show its chnaged
                    $("#listprice_"+i).css("background-color","#F60");  

                    //update totals (notworking)
                    calculateTotal(i);                  

                    return false;
                }

            }); 

            /////////////////////////////////////////////////////////////


        }); 
        alert('loaded '+loadedValues+' prices');


    }});    



    });//end click event
});


/////////////////////////////////////////////////////
//any input field that changes updates the calculation, not working fully i.e load product prices
/////////////////////////////////////////////////////
$(document).ready(function(){   
    $(":input").change(calculateTotal); 
});


////////////////////////////////////
//calculate total
///////////////////////////////////
var calculateTotal = function(index){

    var $discountpercent = null;
    var $total=null;
    var $quantity=null;
    var $id=null;
    var $marginpercent=null;
    var $margintotal=null;
    var myArray=null;

    console.log('index='+index + ' indexlen= '+index.length );  

    if(index === undefined){    
        console.log('getting id');
        //get id of textbox
        $id=$(this).attr('id'); 
        //get the row id
        $id=$id.toString();
        myArray = $id.split('_');   
        $id=myArray[1]; 
    }
    else
    {
        console.log('setting id=index');
        $id=index;      
    }

    var $listprice= $("#listprice_"+$id).val();

    //turn entered number into %
    $discountpercent= $("#discountpercent_"+$id).val()/100; 
    $discountlistprice=$listprice-($listprice*$discountpercent);    

    //turn margin to % (note margin is global)
    $marginpercent= $("#marginpercent_"+$id).val()/100;

    //apply margin % to DLP
    if($discountlistprice>0)
    {
        $margintotal=$discountlistprice+($discountlistprice*$marginpercent);
    }
    else
    {
        $margintotal=0;
    }

    //set rrp
    $rrp=$margintotal;
    $("#rrp_"+$id).val($rrp);   

    //quantity
    $quantity=$("#quantity_"+$id).val();

    //calculate total   
    $total=$quantity*$rrp;

    //set the value
    $("#discountlistprice_"+$id).val($discountlistprice);

    //set the total by changing the total div
    $("#total_"+$id).html($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-01T02:15:24+00:00Added an answer on June 1, 2026 at 2:15 am

    Change your handler to an anonymous function:

    $(document).ready(function(){   
        $(":input").change(function () {
           var index = getIndex(); // however you are getting this value
           calculateTotal(index);
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a function that works when called in IE and Chrome, but it
I've got a function that does (in short): my $file = IO::File->new(| some_command >>
I'm using C structs in objc and I've created a function that assembles the
I've created a javascript function that allows me to validate if one field or
Im really really new to Javascript but Ive got this script that loads the
Does anyone know how the std::sqrt() function works? (or at least have an idea?)
I'm creating a web application that does some very heavy floating point arithmetic calculations,
Ive been having some trouble using Plot to graph a complicated composite function. I
I've got a function that runs a user generated Regex. However, if the user
I've made some function that generates an email template. Code it generates is pure

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.