Forum Moderators: open

Message Too Old, No Replies

Function Undefined?

         

zero3ree

11:19 pm on Oct 30, 2009 (gmt 0)

10+ Year Member



I am writing a tax calc code and I have everything I think...
For some reason it says that my function is undefined on button click.

here is the html:

<html>
<head>
<title>HTML To JavaScript Example</title>
<script src="get.js" language="JavaScript" type="text/javascript">

function getInt(id) {
return parseInt(document.getElementById(id).value);
}

</script>
<script src="code.js" language="JavaScript" type="text/javascript">
</script>
</head>
<body>
<h1>Tax Calculator</h1>

<form>
<p>Taxable Income #1:
<input id="taxFile1" value="5123"/></p>
<p>Taxable Income #2:
<input id="taxFile2" value="20000"/><br></p>
Taxable Income #3:
<input id="taxFile3" value="300000"/></p>
Taxable Income #4:
<input id="taxFile4" value="400000"/></p>
<input type="button" onclick="myButtonClick(myButtonClick())" value="Calculate Taxes"/>

<hr>

</form>
</body>
</html>
Here is the JS

function myButtonClick() {
// Fetch from HTML into JavaScript variables.
var tax1 = getInt(taxFile1);
var tax2 = getInt(taxFile2);
var tax3 = getInt(taxFile3);
var tax4 = getInt(taxFile4);
if ((tax1,tax2,tax3,tax4)<=7,835) {
baseT=.10;
preT1=(tax1*baseT);
t1= preT1.toFixed(2);
preT2=(tax1*baseT);
t2= preT2.toFixed(2);
preT3=(tax1*baseT);
t3= preT3.toFixed(2);
preT4=(tax1*baseT);
t4= preT4.toFixed(2);

}
else
if ((tax1;tax2;tax3;tax4>7,835) && (tax1;tax2;tax3;tax4<=31,850)) {
baseT= 782.50;
preT1=(tax1-7,835).15)+ baseT;
t1= preT1.toFixed(2);
preT2=(tax2-7,835).15)+ baseT;
t2= preT2.toFixed(2);
preT3=(tax3-7,835).15)+ baseT;
t3= preT3.toFixed(2);
preT4=(tax4-7,835).15)+ baseT;
t4= preT4.toFixed(2);

}
else
if ((tax1;tax2;tax3;tax4>31,850) && (tax1;tax2;tax3;tax4<=77,100)) {
baseT= 782.50;
preT1=(tax1-31,850).25)+ baseT;
t1= preT1.toFixed(2);
preT2=(tax2-31,850).25)+ baseT;
t2= preT2.toFixed(2);
preT3=(tax3-31,850).25)+ baseT;
t3= preT3.toFixed(2);
preT4=(tax4-31,850).25)+ baseT;
t4= preT4.toFixed(2);

else
if ((tax1;tax2;tax3;tax4>77,100) && (tax1;tax2;tax3;tax4<=160,850)) {
baseT= 782.50;
preT1=(tax1-77,100).28)+ baseT;
t1= preT1.toFixed(2);
preT2=(tax2-77,100).28)+ baseT;
t2= preT2.toFixed(2);
preT3=(tax3-77,100).28)+ baseT;
t3= preT3.toFixed(2);
preT4=(tax4-77,100).28)+ baseT;
t4= preT4.toFixed(2);

}
else
if ((tax1;tax2;tax3;tax4>160,850) && (tax1;tax2;tax3;tax4<=349,700)) {
baseT= 782.50;
preT1=(tax1-160,850).33)+ baseT;
t1= preT1.toFixed(2);
preT2=(tax2-160,850).33)+ baseT;
t2= preT2.toFixed(2);
preT3=(tax3-160,850).33)+ baseT;
t3= preT3.toFixed(2);
preT4=(tax4-160,850).33)+ baseT;
t4= preT4.toFixed(2);

}
else
if ((tax1;tax2;tax3;tax4>349,700) && (tax1;tax2;tax3;tax4<=31,850)) {
baseT= 782.50;
preT1=(tax1-349,700).15)+ baseT;
t1= preT1.toFixed(2);
preT2=(tax2-349,700).15)+ baseT;
t2= preT2.toFixed(2);
preT3=(tax3-349,700).15)+ baseT;
t3= preT3.toFixed(2);
preT4=(tax4-349,700).15)+ baseT;
t4= preT4.toFixed(2);

}
{
return = document.write(+t1+)
document.write(+t2+)
document.write(+t3+)
document.write(+t4+)
}
}

As a beginner I have put alot of time in this and Im stuck.
Just need a push in the right direction.

lavazza

2:43 am on Oct 31, 2009 (gmt 0)

10+ Year Member



Instead of
<input type="button" onclick="myButtonClick(myButtonClick())" value="Calculate Taxes"/>

Try
<input type="button" onclick="myButtonClick();" value="Calculate Taxes"/>

=========================

Instead of
else
if ((tax1;tax2;tax3;tax4>7,835) && (tax1;tax2;tax3;tax4<=31,850)) {

Try
else
if ((tax1 >7835 ¦¦ tax2 >7835 ¦¦ tax3 >7835 ¦¦ tax4>7835) && (tax1 >7835 ¦¦ tax2 >7835 ¦¦ tax3 >7835 ¦¦ tax4<=31,850)) {

Repeat for the other three 'else if' conditions, too

Note that the WebmasterWorld software stuffs up the pipe symbols {¦¦)

=========================

Instead of
preT1=(tax1-7,835).15)+ baseT;

Try
preT1=(tax1-7,835)*15+ baseT;

Repeat quite a few times

==================

Instead of
{
return = document.write(+t1+)
document.write(+t2+)
document.write(+t3+)
document.write(+t4+)
}

Try
{
return {document.write(t1);
document.write(t2);
document.write(t3);
document.write(t4);
};
}

zero3ree

3:04 pm on Oct 31, 2009 (gmt 0)

10+ Year Member



Thank you. I made all those changes and for some reason I am still getting an undefined error.
here is what I have now....

JS:
function myButtonClick() {
// Fetch from HTML into JavaScript variables.
var tax1 = getInt(taxFile1);
var tax2 = getInt(taxFile2);
var tax3 = getInt(taxFile3);
var tax4 = getInt(taxFile4);
if (tax1<=7835 ¦¦ tax2<=7835 ¦¦ tax3<=7835 ¦¦ tax4<=7835) {
baseT=.10;
preT1=(tax1*baseT);
t1= preT1.toFixed(2);
preT2=(tax1*baseT);
t2= preT2.toFixed(2);
preT3=(tax1*baseT);
t3= preT3.toFixed(2);
preT4=(tax1*baseT);
t4= preT4.toFixed(2);

}
else
if ((tax1 >7835 ¦¦ tax2 >7835 ¦¦ tax3 >7835 ¦¦ tax4>7835) && (tax1 <=31,850 ¦¦ tax2 <=31,850 ¦¦ tax3 <=31,850 ¦¦ tax4<=31,850)) {
baseT= 782.50;
preT1=(tax1-7,835)*.15 + baseT;
t1= preT1.toFixed(2);
preT2=(tax2-7,835)*.15 + baseT;
t2= preT2.toFixed(2);
preT3=(tax3-7,835)*.15 + baseT;
t3= preT3.toFixed(2);
preT4=(tax4-7,835)*.15 + baseT;
t4= preT4.toFixed(2);

}
else
if ((tax1 >31,850 ¦¦ tax2 >31,850 ¦¦ tax3 >31,850 ¦¦ tax4>31,850) && (tax1<=77,100 ¦¦ tax2<=77,100¦¦ tax3<=77,100 ¦¦ tax4<=77,100)){
baseT= 782.50;
preT1=(tax1-31,850)*.25 + baseT;
t1= preT1.toFixed(2);
preT2=(tax2-31,850)*.25 + baseT;
t2= preT2.toFixed(2);
preT3=(tax3-31,850)*.25 + baseT;
t3= preT3.toFixed(2);
preT4=(tax4-31,850)*.25 + baseT;
t4= preT4.toFixed(2);

else
if ((tax1 >77,100 ¦¦ tax2 >77,100 ¦¦ tax3 >77,100 ¦¦ tax4>77,100) && (tax1<=160,850 ¦¦ tax2<=160,850¦¦ tax3<=160,850 ¦¦ tax4<=160,850))(
baseT= 782.50;
preT1=(tax1-77,100)*.28 + baseT;
t1= preT1.toFixed(2);
preT2=(tax2-77,100)*.28 + baseT;
t2= preT2.toFixed(2);
preT3=(tax3-77,100)*.28 + baseT;
t3= preT3.toFixed(2);
preT4=(tax4-77,100)*.28 + baseT;
t4= preT4.toFixed(2);

}
else
if ((tax1 >160,850 ¦¦ tax2 >160,850 ¦¦ tax3 >160,850 ¦¦ tax4>160,850) && (tax1<=349,700 ¦¦ tax2<=349,700¦¦ tax3<=349,700 ¦¦ tax4<=349,700))(
baseT= 782.50;
preT1=(tax1-160,850)*.33 + baseT;
t1= preT1.toFixed(2);
preT2=(tax2-160,850)*.33 + baseT;
t2= preT2.toFixed(2);
preT3=(tax3-160,850)*.33 + baseT;
t3= preT3.toFixed(2);
preT4=(tax4-160,850)*.33 + baseT;
t4= preT4.toFixed(2);

}
else
if ((tax1 >349,700 ¦¦ tax2 >349,700 ¦¦ tax3 >349,700 ¦¦ tax4>349,700){
baseT= 782.50;
preT1=(tax1-349,700)*.35 + baseT;
t1= preT1.toFixed(2);
preT2=(tax2-349,700)*.35 + baseT;
t2= preT2.toFixed(2);
preT3=(tax3-349,700)*.35 + baseT;
t3= preT3.toFixed(2);
preT4=(tax4-349,700)*.35 + baseT;
t4= preT4.toFixed(2);

}
{
return = document.write(t1);
document.write(t2);
document.write(t3);
document.write(t4);
};
}

HTML:
<html>
<head>
<title>HTML To JavaScript Example</title>
<script src="get.js" language="JavaScript" type="text/javascript">

function getInt(id) {
return parseInt(document.getElementById(id).value);
}

</script>
<script src="code.js" language="JavaScript" type="text/javascript">
</script>
</head>
<body>
<h1>Lab2</h1>

<form>
<p>Taxable Income #1:
<input id="taxFile1" value="5123"/></p>
<p>Taxable Income #2:
<input id="taxFile2" value="20000"/><br></p>
Taxable Income #3:
<input id="taxFile3" value="300000"/></p>
Taxable Income #4:
<input id="taxFile4" value="400000"/></p>
<input type="button" onclick="myButtonClick()" value="Calculate Taxes"/>

<hr>

</form>
</body>
</html>

astupidname

8:08 pm on Oct 31, 2009 (gmt 0)

10+ Year Member



Take the commas out of all the numbers everywhere in your code. In code a comma denotes a listing of seperate individual properties. Such as 349,700 in code that is two different numbers seperated by a comma. Should be 349700 if it is one number. So strip the commas out of ALL your numbers and see if that helps.

zero3ree

8:27 pm on Oct 31, 2009 (gmt 0)

10+ Year Member



That was it! Thank you very much for the help.

astupidname

8:32 pm on Oct 31, 2009 (gmt 0)

10+ Year Member



Also, taxFile1, taxFile2 taxFile3 & taxFile 4 are id's and should be quoted when you are passing them to the getInt function. Also, don't use document.write after the page has loaded, such as what you are doing when clicking the button. Rather use document.getElementById("an_Elements_Id_Here").value = your_variables_value_here. Note only use .value there if the output element is a form input or textarea, use .innerHTML if it is a paragraph or div element. And the statement: return = is wrong also, the equals sign does not belong there as you can not assign a variable named 'return'. Just use: return the_value_to_return_here;

zero3ree

1:03 am on Nov 1, 2009 (gmt 0)

10+ Year Member



Yeah the only return I am getting is the t1. I believe I need to make all values going back to html getId? I am trying to use a div but i really don't understand it fully.
Thank you again.

zero3ree

6:26 pm on Nov 1, 2009 (gmt 0)

10+ Year Member



Sorry to keep bugging but I got these div templates and I am trying to set this up so that the buttons call out to the div js and return the values from the tax js to the html under the <hr>. I am not to comfortable with div. Can you look at this and see if I am even remotely close to what I need?

div.js:
function setDiv(divID) {
var div = document.getElementById(t1);
div.innerHTML = "";
div.style.display="block";
}
function setDiv(divId, sHtml) {
var div = document.getElementById(divId);
div.innerHTML = sHtml;
div.style.display="block";
}
function setDiv(divId, sHTMLtoAdd) {
var div = document.getElementById(divId);
div.innerHTML += sHTMLtoAdd;
div.style.display="block";
}
divTest js:
function taxButton1(t1)
{
setDiv(divId, "Taxes #1=" + taxes1);
}

function taxButton2(t2){
setDiv(divId, "Taxes #2=" + taxes2);
}
function taxButton3(t3)
{
setDiv(divId, "Taxes #3=" + taxes3);
}
function taxButton4(t4)
{
setDiv(divId, "Taxes #4=" + taxes4);
}
revised tax js:
function myButtonClick(taxes1,taxes2,taxes3,taxes4) {
// Fetch from HTML into JavaScript variables.
var tax1 = getInt('taxFile1');
var tax2 = getInt('taxFile2');
var tax3 = getInt('taxFile3');
var tax4 = getInt('taxFile4');
if (tax1<=7835 ¦¦ tax2<=7835 ¦¦ tax3<=7835 ¦¦ tax4<=7835) {
baseT=.10;
preT1=(tax1*baseT);
t1= preT1.toFixed(2);
preT2=(tax1*baseT);
t2= preT2.toFixed(2);
preT3=(tax1*baseT);
t3= preT3.toFixed(2);
preT4=(tax1*baseT);
t4= preT4.toFixed(2);

}
else
if ((tax1 >7835 ¦¦ tax2 >7835 ¦¦ tax3 >7835 ¦¦ tax4>7835) && (tax1 <=31,850 ¦¦ tax2 <=31,850 ¦¦ tax3 <=31,850 ¦¦ tax4<=31,850)) {
baseT= 782.50;
preT1=(tax1-7835)*.15 + baseT;
t1= preT1.toFixed(2);
preT2=(tax2-7835)*.15 + baseT;
t2= preT2.toFixed(2);
preT3=(tax3-7835)*.15 + baseT;
t3= preT3.toFixed(2);
preT4=(tax4-7835)*.15 + baseT;
t4= preT4.toFixed(2);

}
else
if ((tax1 > 31850 ¦¦ tax2 > 31850 ¦¦ tax3 > 31850 ¦¦ tax4 > 31850) && (tax1 <= 77100 ¦¦ tax2 <= 77100 ¦¦ tax3 <= 77100 ¦¦ tax4 <= 77100)) {
baseT = 4386.25;
preT1 = (tax1 - 31850) * .25 + baseT;
t1 = preT1.toFixed(2);
preT2 = (tax2 - 31850) * .25 + baseT;
t2 = preT2.toFixed(2);
preT3 = (tax3 - 31850) * .25 + baseT;
t3 = preT3.toFixed(2);
preT4 = (tax4 - 31850) * .25 + baseT;
t4 = preT4.toFixed(2);
}

else
if ((tax1 > 77100 ¦¦ tax2 > 77100 ¦¦ tax3 > 77100 ¦¦ tax4 > 77100) && (tax1 <= 160850 ¦¦ tax2 <= 160850 ¦¦ tax3 <= 160850 ¦¦ tax4 <= 160850)) {
baseT = 15698.75;
preT1=(tax1-77100)*.28 + baseT;
t1= preT1.toFixed(2);
preT2=(tax2-77100)*.28 + baseT;
t2= preT2.toFixed(2);
preT3=(tax3-77100)*.28 + baseT;
t3= preT3.toFixed(2);
preT4=(tax4-77100)*.28 + baseT;
t4= preT4.toFixed(2);

}
else
if ((tax1 >160850 ¦¦ tax2 >160850 ¦¦ tax3 >160850 ¦¦ tax4>160850) && (tax1<=349700 ¦¦ tax2<=349700¦¦ tax3<=349700 ¦¦ tax4<=349700)){
baseT= 39148.75;
preT1=(tax1-160850)*.33 + baseT;
t1= preT1.toFixed(2);
preT2=(tax2-160850)*.33 + baseT;
t2= preT2.toFixed(2);
preT3=(tax3-160850)*.33 + baseT;
t3= preT3.toFixed(2);
preT4=(tax4-160850)*.33 + baseT;
t4= preT4.toFixed(2);

}
else
if ((tax1 >349700 ¦¦ tax2 >349700 ¦¦ tax3 >349700 ¦¦ tax4>349700)){
baseT= 101469.25;
preT1=(tax1-349700)*.35 + baseT;
t1= preT1.toFixed(2);
preT2=(tax2-349700)*.35 + baseT;
t2= preT2.toFixed(2);
preT3=(tax3-349700)*.35 + baseT;
t3= preT3.toFixed(2);
preT4=(tax4-349700)*.35 + baseT;
t4= preT4.toFixed(2);

}
{
document.getElementById('taxes1').value=t1
document.getElementById('taxes2').value=t2
document.getElementById('taxes3').value=t3
document.getElementById('taxes4').value=t4
};
}
And HTML:
<html>
<head>
<title>HTML To JavaScript Example</title>
<script src="get.js" language="JavaScript" type="text/javascript">
function getInt(id) {
return parseInt(document.getElementById(id).value);
}</script>
<script src="tax.js" language="JavaScript" type="text/javascript"></script>
<script src="div.js" language="JavaScript" type="text/javascript"></script>
<script src="divTest.js" language="JavaScript" type="text/javascript"></script>
</head>
<body>
<h1>Lab2</h1>

<form>
<p>Taxable Income #1:
<input id="taxFile1" value="5123"/></p>
<p>Taxable Income #2:
<input id="taxFile2" value="20000"/><br></p>
Taxable Income #3:
<input id="taxFile3" value="300000"/></p>
Taxable Income #4:
<input id="taxFile4" value="400000"/></p>
<input type="button" value="Tax 1" onclick="taxButton1('outputDivId');"/>
<input type="button" value="Tax 2" onclick="taxButton2('outputDivId');"/>
<input type="button" value="Tax 3" onclick="taxButton3('outputDivId');"/>
<input type="button" value="Tax 4" onclick="taxButton4('outputDivId');"/>
<hr>
<div id="outputDivId" name="output" style="display: block;">

</form>
</body>
</html>

Thank you for all your help.