user2108839
user2108839

Reputation: 17

Using Javascript and HTML5 output but am not getting output

I am building a calculator that gives the state specific sales tax of a real estate transaction. I know my "normalrtfCalc" function works but my issue is getting the "amount" from the form into the function and the result into the output. Any help would be greatly appreciated. Thanks!

Here is my HTML:

<form id="rtfCalc" oninput="updateOutput( )">
    <input name="sale amount" type="number" value="0" />
    <output name="transfer fee" for="sale amount"></output>
</form>

Here is my JS:

function updateOutput() {
    var form = document.getElementById("rtfCalc");
    var out = form.elements["transfer fee"];
    var amount = parseInt(form.elements["sale amount"].value);

    function normalrtfCalc(amount) {
        if (amount <= 150000) {
            out.value = Math.ceil(amount / 500) * 2;
        } else if (amount <= 350000) {
            if ((amount - 150000) <= 50000) {
                out.value = 600 + (Math.ceil((amount - 150000) / 500) * 3.35);
            } else {
                out.value = 935 + (Math.ceil((amount - 200000) / 500) * 3.9);
            }
        } else {
            if ((amount - 200000) <= 350000) {
                out.value = 2735 + (Math.ceil((amount - 200000) / 500) * 4.8);
            } else if ((amount - 550000) <= 300000) {
                out.value = 4655 + (Math.ceil((amount - 555000) / 500) * 5.3);
            } else if ((amount - 850000) <= 150000) {
                out.value = 7835 + (Math.ceil((amount - 850000) / 500) * 5.8);
            } else {
                out.value = 9575 + (Math.ceil((amount - 1000000) / 500) * 6.05);
            }
        }
    }
};

Upvotes: 0

Views: 254

Answers (1)

Daedalus
Daedalus

Reputation: 7722

There are several things wrong with your code. I will post it below and explain in comments:

function updateOutput() {
    var form = document.getElementById("rtfCalc");
    var out = form.elements["transfer_fee"];
    var amount = parseInt(form.elements["sale_amount"].value);

    function normalrtfCalc(amount) { // an equal sign(=) before the opening curly bracket is invalid syntax; remove it, and execute the function as stated below, and your code works.

        if (amount <= 150000) {
            out.value = Math.ceil(amount / 500) * 2;

        } else if (amount <= 350000) {
            if ((amount - 150000) <= 50000) {
                out.value = 600 + (Math.ceil((amount - 150000) / 500) * 3.35);
            } else {
                out.value = 935 + (Math.ceil((amount - 200000) / 500) * 3.9);
            }
        } else {
            if ((amount - 200000) <= 350000) {
                out.value = 2735 + (Math.ceil((amount - 200000) / 500) * 4.8);
            } else if ((amount - 550000) <= 300000) {
                out.value = 4655 + (Math.ceil((amount - 555000) / 500) * 5.3);
            } else if ((amount - 850000) <= 150000) {
                out.value = 7835 + (Math.ceil((amount - 850000) / 500) * 5.8);
            } else {
                out.value = 9575 + (Math.ceil((amount - 1000000) / 500) * 6.05);
            }
        }
    }
    normalrtfCalc(amount); //you have to call the function in order for it to execute.
};

DEMO

Upvotes: 1

Related Questions