Forum Moderators: open
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.
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);
};
}
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>
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.