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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:14:44+00:00 2026-05-26T02:14:44+00:00

I made a form which I make calculation. If I make this PHP it’s

  • 0

I made a form which I make calculation. If I make this PHP it’s not that hard. But I’m planning to make it Jquery or JavaScript so people whoever puts number into this it can calculate right away they don`t have to click any button. Is there anybody can help me with this ?

http://jsfiddle.net/gXYg5/

  • 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-05-26T02:14:45+00:00Added an answer on May 26, 2026 at 2:14 am

    OK, I wrote this and you can see it in action here: http://jsfiddle.net/jfriend00/b9dUn/

    Here’s what the code looks like. The code could have been a lot more compact, but since you don’t have meaningful field names, I used lots of intermediate local variables to make the code a lot more readable.

    $("#calc").click(function() {
        var salesPrice = getNum("textfield");
        var tradeIn = getNum("textfield2");
        var subTotal1 = salesPrice - tradeIn;
        setNum("textfield3", subTotal1, "dollar");
    
        var docFee = getNum("textfield4");
        var subTotal2 = docFee + subTotal1;
        setNum("textfield5", subTotal2, "dollar");
    
        var salesTax = getNum("textfield6", "percent");
        var license = getNum("textfield7");
        var subTotal3 = (subTotal2 * (1 + salesTax)) + license;
        setNum("textfield8", subTotal3, "dollar");
    
        var cashDown = getNum("textfield9");
        var rebate = getNum("textfield10");
        var amountFinanced = subTotal3 - cashDown - rebate;
        setNum("textfield11", amountFinanced, "dollar");
    
        var interestRate = getNum("textfield12", "percent");
        var term = getNum("textfield13");
    
        var payment = (amountFinanced * interestRate) / (1 - Math.pow(1 + interestRate, -term));
        setNum("textfield14", payment, "dollar");
    
    });
    
    function addCommas(nStr)
    {
        nStr += '';
        x = nStr.split('.');
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
    }
    
    function getNum(id, type) {
        try {
            var raw = document.getElementById(id).value;
            raw = raw.replace(/\$|\s|%|,/g, "");
            raw = Number(raw);
            if (isNaN(raw)) {
                raw = 0;
            }
            if (type === "percent") {
                raw = raw / 100;
            }
            return(raw);
        } catch(e) {
            return(0);
        }
    }
    
    function setNum(id, val, type) {
        if (type === "dollar") {
            val = val.toFixed(2);
            val = "$" + addCommas(val);
        } else if (type === "percent") {
            val = (val * 100).toFixed(2) + "%";
        }
        document.getElementById(id).value = val;
    }
    
    // fill in some default values for testing:
    setNum("textfield", 20000, "dollar");  // sales price
    setNum("textfield2", 5000, "dollar");  // tradein
    setNum("textfield4", 12, "dollar");  // doc fee
    setNum("textfield6", 0.075, "percent");  // sales tax
    setNum("textfield7", 75, "dollar");  // license
    setNum("textfield9", 20, "dollar");  // cash down
    setNum("textfield10", 40, "dollar");  // rebate
    setNum("textfield12", 0.035, "percent");  // interest rate
    setNum("textfield13", 72);  // month term
    

    The coding would be lot less errorprone if you gave the fields meaningful names like id="cashDown" instead of id="textfield9". I obtained the loan payment formula from here. You should verify that it seems correct to you. I made the calculated fields readonly so they cannot be manipulated by typing and I gave them a different color so you can more easily see them after hitting the calculate button.

    I have not checked this in detail to make sure every part of the calculation is proper and all the input error handling is as desired. You should do that to your satisfaction. This should give you the general idea how to do this.

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

Sidebar

Related Questions

I have started using jQuery and rails. I have made a simple form that
Say that I have made some software (which has both library form and can
I have made a form for client registration, and I can't make this postvalidators
I have a form made up of multiple, optional subparts - each of which
I have made a class which a form can inherit from and it handles
I made a form class in c# that has a devexpress grid, a label
I'm setting up a form in which multiple entries can potentially be made (via
I am trying to create a car make/model form using Javascript or AJAX, problem
i have a rails form ive made in which the form fields are extracted
I did the following in MS Access: I made a form which had 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.