JavaScript - Calculator Problems
i am building/coding a simple calculator and i have this
Code: function addDigit() { if(id=7) { document.getElementById("display").value="7"; } if(id=8) { document.getElementById("display").value="8"; } if(id=9) { document.getElementById("display").value="9"; } } function clearAll() { document.getElementById("display").value=""; } as the code but when i press the 7 8 or 9 buttons it only shows up as a 9. go to http://www.mightypeacock.com/Practice/calculator.html to see what i mean. Similar TutorialsNew problem in post #5. Okay, so I'm dipping my hand into JavaScript for the first time now, using it to try and code a calculator for my lab. Basically, it needs to take some numbers as input, do some simple calculations, and spit out some output. I'm rather new to accepting input and passing output so I've been struggling with these parts. I've finally gotten my code to do both, but only in FireFox. IE fails to give output and I'm not sure why. Here's some of the code with unnecessary bits (CSS, text, etc.) removed: Code: <html> <head> <script type="text/javascript" src="functions2.js"> function oneFert(recArr,fertArr) { /* Calculates the amount of fertilizer needed to meet all recommendations using one fertilizer. */ var amtArr = [recArr[0]/fertArr[0],recArr[1]/fertArr[1],recArr[2]/fertArr[2]]; var lbs = 0; for(x in amtArr) { if(amtArr[x]>lbs) lbs=amtArr[x]; } return lbs; } function toString(lbs,fertArr,area) { return "You need to apply "+Math.round(lbs*100)+" lbs of "+fertArr[0]+"-"+fertArr[1]+"-"+fertArr[2]+" for every 1,000 square feet. You will need a <b>total of "+Math.round(lbs*100)*area/1000+" lbs</b> of "+fertArr[0]+"-"+fertArr[1]+"-"+fertArr[2]+" for an area of "+area+" square feet."; } </script> </head> <body> <form name="in"> Recommendation<br /> N:<input type="text" name="recn" /> P:<input type="text" name="recp" /> K:<input type="text" name="reck" /> per 1,000 square feet<br /> Fertilizer<br /> N:<input type="text" name="fertn" /> P:<input type="text" name="fertp" /> K:<input type="text" name="fertk" /> (%)<br /> Area<br /><input type="text" name="area" /> square feet (no commas) <br /><br /> <script type="text/javascript"> <!-- var recArr = [document.in.recn.value,document.in.recp.value,document.in.reck.value]; var fertArr = [document.in.fertn.value,document.in.fertp.value,document.in.fertk.value]; var lbs = 0; var area = document.in.area.value; lbs = oneFert(recArr,fertArr); document.write(toString(lbs,fertArr,area)); // End hiding --> </script> <br /> <input type="button" value="Calculate" onclick="history.go(0)" style="width:auto;" /> </form> </body> </html> I'd also appreciate other comments on the code not relating to this one problem. I'm sure there's better ways of doing much of what I've done. (And mind you, this is a very basic prototype.) Also, does anyone know of some good debugging tools for IE? I just found out about Firebug for FF and started using that. Hi all, I am creating a simple 3 field calculator that calculates net, vat and gross. The user has to input 2 of the 3 fields, hit calculate and it should fill the 3rd field. I have so far succeeded with the net field (enter in gross and vat) but the other 2 throw up spurious results! I have worked out these equations (they work on a calculator) Net = G - (( G * V ) / ( V + 100 )) VAT = (( G - N ) x 100 ) / N Gross = N + ( N x ( V / 100 )) Heres the code for the javascript Code: <script type="text/javascript"> function netvatgross_calc() { var net = (Number(document.calc.gross.value))-(((Number(document.calc.gross.value)) * (Number(document.calc.vat.value)))/((Number(document.calc.vat.value))+100)); var net = net.toFixed(2); document.calc.net.value=net; var vat = (((Number(document.calc.gross.value))-(Number(document.calc.net.value)))*100)/(Number(document.calc.net.value)); var vat = vat.toFixed(2); document.calc.vat.value=vat; var gross = (Number(document.calc.net.value))+((Number(document.calc.vat.value))/100)*(Number(document.calc.net.value)); var gross = gross.toFixed(2); document.calc.gross.value=gross; } </script> Here's the work in progress I am also wanting to add some code to throw up an error if less or more than 2 values have been entered. What am I doing wrong? Any help greatly appreciated! Cheers Dan How could i make a calculator that changes three values when a vertex on an image moves. Example : When a point moves on the triangle it changes the cost quality and time. I have this start and need the meat for the concept to work.<!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <p><img src="Assets/Project-triangle.svg.png" width="500" height="492" alt=""/>-------------------------------------------<img src="Assets/triangle.gif" width="224" height="210" alt=""/></p> <p> <label for="textfield">Cost :</label> <input type="text" name="textfield" id="textfield"> <label for="textfield2"> Time :</label> <input type="text" name="textfield2" id="textfield2"> <label for="textfield3">Quality :</label> <input type="text" name="textfield3" id="textfield3"> </p> <p> </p> </body> </html> Help would be greatly appreciated! Below is the code for a calculator using script. can one help me to explain me the colored line?? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> </body> <script language="javascript" type="text/javascript"> function multiply() { a=Number Quote: Quote: (document.calculator.number1.value) ; b=Number(document.calculator.number2.value); c=a*b; document.calculator.total.value=c; } function addition(){ a=Number(document.calculator.number1.value); b=Number(document.calculator.number2.value); c=a+b; document.calculator.total.value=c; } function subtraction(){ a=Number(document.calculator.number1.value); b=Number(document.calculator.number2.value); c=a-b; document.calculator.total.value=c; } function division(){ a=Number(document.calculator.number1.value); b=Number(document.calculator.number2.value); c=a/b; document.calculator.total.value=c; } function modulus(){ a=Number(document.calculator.number1.value); b=Number(document.calculator.number2.value); c=a%b; document.calculator.total.value=c; } </script> </head> <body> <form name="calculator"> Number 1: <input type="text" name="number1"> Number 2: <input type="text" name="number2"> Get Result: <input type="text" name="total"> <input type="button" value="ADD" onclick="javascript:addition();"> <input type="button" value="SUB" onclick="javascript:subtraction();"> <input type="button" value="MUL" onclick="javascript:multiply();"> <input type="button" value="DIV" onclick="javascript:division();"> <input type="button" value="MOD" onclick="javascript:modulus();"> </form> </body> </html> I'm using the following code to calculate an age from the given date of birth however it works for some DOB's, gives wrong answers for some and undefined for others, can someone help me? Code: <div style="font-size:13px; font-family:Verdana;"> <script> <!-- /*Darren McGrady */ var current= new Date() var day = current.getDate() var month = current.getMonth() + 1 var year = current.getFullYear() var a = 29 var b = 12 var c = 1980 var age if (month < b) age = (year-c)-1 if ((month == b) && (day < a)) age = (year-c)-1 else age = year-c document.write(age); //--> </script> </div> Hi all. I'm having a bit of a problem returning a different value each time a different button is pressed for calculator. What am I doing wrong? Code: <script> function calc(btname){ for(var x=0;x<=11;x++){ if(x==0){ btname="zero"; }else if(x==1){ btname="one"; }else if(x==2){ btname="two"; }else if(x==3){ btname="three"; }else if(x==4){ btname="four"; }else if(x==5){ btname="five"; }else if(x==6){ btname="six"; }else if(x==7){ btname="seven"; }else if(x==8){ btname="eight"; }else if(x==9){ btname="nine"; }else if(x==10){ btname="multiply"; }else if(x==11){ btname="divide"; } if('document.forms.test.'+btname+'.onClick'){ alert(btname); } } } function scalc(){ var sign=""; var btname=""; document.write('<form name="test">'); for(var x=0;x<=11;x++){ if(x==0){ btname="zero"; }else if(x==1){ btname="one"; }else if(x==2){ btname="two"; }else if(x==3){ btname="three"; }else if(x==4){ btname="four"; }else if(x==5){ btname="five"; }else if(x==6){ btname="six"; }else if(x==7){ btname="seven"; }else if(x==8){ btname="eight"; }else if(x==9){ btname="nine"; }else if(x==10){ btname="multiply"; sign = "*"; }else if(x==11){ btname="divide"; sign = "/"; } if(x<10){ document.write('<input type="button" size="30" name="'+btname+'" value="'+x+'" onClick="calc(this);"> '); }else if(x>9 && x<12){ document.write('<input type="button" size="30" name="'+btname+'" value="'+sign+'" onClick="calc(this);"> '); } if(x==2 || x == 5 || x == 8){ document.write('<br>'); } } document.write('</form>'); } scalc(); </script> ANY help is GREATLY appreciated! Hi , this is my bmi calculator script <HEAD> <script type="text/javascript"> <!-- Begin script of spillo3000 function calculateBMI() { var weight = eval(document.form.weight.value) var height = eval(document.form.height.value) var height2 = height / 100 var BMI = weight / (height2 * height2) if(weight == "" || isNaN(weight) || height2 == "" || isNaN(height2)) { alert("inserisci un valore"); return; } else { document.form.BodyMassIndex.value=custRound(BMI,1); if(BMI < 18.5) { document.getElementById('feedback').innerHTML = 'Underweight '; }else if(BMI >=18.5 && BMI < 29.9) { document.getElementById('feedback').innerHTML = 'Normal '; }else if(BMI > 29.9) { document.getElementById('feedback').innerHTML = 'Overweight '; } } } function custRound(x,places) { return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places) } function resetAll(){ document.getElementById('feedback').innerHTML=""; return true; } // End --> </script> </HEAD> <!-- --> <BODY> <div align="left"> <form name="form" id="form"> <input type="Text" name="weight" size="4"> Peso (Kg)<br> <input type="Text" name="height" size="4"> Altezza (Cm)<br> <input type="Text" name="BodyMassIndex" id="BodyMassIndex" size="4"> BMI<br> <input type="Text" name="feedback" id="feedback" style="padding:10px 0 20px 0; border:none; font-weight:600; color:#555555; font-family:verdana;" ><br> <input type="button" style="font-size: 8pt" value="Calcola" onClick="calculateBMI()" name="button"> <input type="reset" style="font-size: 8pt" value="Reset" onclick="resetAll()" name="button"> </form> </div> <!-- Script Size: I would want to show writing that it says if is in overweight, norms or I underweigh, but the script does not work. could someone help me? please I'm currently revamping a website that was written in aspx. The problem i'm having is that the webpage is moving to a hosting company that doesn't support asp. I'm wanting to rebuild the calculator in javascript, however I have no javascript experience. I can figure some parts of the code out, however I don't really have time to learn it, and was wondering if someone would be willing to help. The site i'm trying to change is: http://sunflowertitle.com/ratecalculator.aspx It shouldn't be that difficult to rebuild as its not a detailed calculator. I have all the original files if anyone needs them, i've been working on this for awhile and i'm to the point i'd be willing to pay someone to build it. I'm not sure what the code is to add this to javascript. I don't know if anyone can help me with this? Each numeric button should change the input box up at the top to whatever. When one of the operator buttons (+,-,*,/) is pressed, the value in the input box at the top should change to 0. Before an operator button is pressed, the value should be stored in a variable to keep track of the left-hand side of the equation. After it is pressed, the value should be stored in the right hand side variable. Thanks I want the calculator to take what was clicked display the item number and also the cost. (if possible i'd like to be able to display cost with no button) sorry if the code is messy i'm new to javascript. Code: <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Definitions var in2 = "" var in3 = "" //costs var $mat = "" var cmat = Number($mat) var $sqft = "" var csf = Number($sqft) var chngcost = cmat*csf var $coga = 30 var $cos = 40 var $coshp = 15 var $total = chngcost + $coga + $cos + $coshp // Add a letter to string function itemnumber2(numb) { in2 = numb document.myform.i2.value = in2 document.itemnumber.i2.value = in2 if (in2 == "10") { $mat = 3 document.myform.cmat1.value = $mat } if (in2 == "11") { $mat = 6 document.myform.cmat1.value = $mat } if (in2 == "12") { $mat = 12 document.myform.cmat1.value = $mat } } function itemnumber3(numb) { in3 = numb document.myform.i3.value = in3 document.itemnumber.i3.value = in3 if (in3 == "1") { $sqft = 7.5 document.myform.sqrf1.value = $sqft } if (in3 == "2") { $sqft = 5.5 document.myform.sqrf1.value = $sqft } if (in3 == "3") { $sqft = 5 document.myform.sqrf1.value = $sqft } } //total cost function function totalcost() { ttc1 = $total document.myform.ttc.value = ttc1 } </SCRIPT> Code: <input type="radio" id="10" name="Size" value="18x22" onclick="itemnumber2(10)"> 18" X 22"<br> <input type="radio" id="11" name="Size" value="24x36" onclick="itemnumber2(11)"> 24" X 36"<br> <input type="radio" id="12" name="Size" value="36x48" onclick="itemnumber2(12)"> 36" X 48"<br> Code: <input type="radio" id="1" name="Material" value="Photo Gloss" onclick="itemnumber3(1)"> Photo Gloss<br> <input type="radio" id="2" name="Material" value="Bond Laminate" onclick="itemnumber3(2)"> Bond Laminate<br> <input type="radio" id="3" name="Material" value="Photo Gloss" onclick="itemnumber3(3)"> Vinyl<br> Code: <form name="itemnumber" action="#"> <span class="style6">Item # <input type="text" size="3" name="i1" value="101" disabled>- <input type="text" size="2" name="i2" disabled>-<input type="text" size="1" name="i3" disabled></span></p> </form> Code: <input name="cost" type="button" onclick="totalcost()" value="Cost"> $<input type="text" size="5" name="ttc" disabled> I have my first ever scholastic JS assignment and so far it's felt nearly impossible to get a grip on how to do it. I've gone through the W3c tutorials entirely 3 times now and still, nothing is clicking mentally. Generally I do better if I can reverse engineer already written code but I can't find anything similar enough to this on the net to figure it out. The assignment is to create a grade averages calculator as seen in this pictu Along with this starter code: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Assignments score calculator</title> <style type="text/css"> <!-- .score { width: 30px; } --> </style> </head> <body> <h1>Assignments score calculator</h1> <form id="calculator" name="calculator" method="post" action=""> <p>Enter your Name: <label for="firstname"></label> <br /> <input type="text" name="firstname" id="firstname" /> <p> <label for="A1">Assignment #1 sco </label> <input name="A1" type="text" class="score" id="A1" maxlength="3" /><br /> <label for="A2">Assignment #2 sco </label> <input name="A2" type="text" class="score" id="A2" maxlength="3" /><br /> <label for="A3">Assignment #3 sco </label> <input name="A3" type="text" class="score" id="A3" maxlength="3" /> <p> <label for="button"></label> <input type="submit" name="Submit" id="Submit" value="Submit" xx="doCalculation(this.form.firstname.value,this.form.A1.value,this.form.A2.value,this.form.A3.value)" /> </form> </body> </html> I understand what I need to do, just not the syntax to do it. Any help would be appreciated. Sorry for newbiness. already solved
I have a formula done in PHP. Lets say it's PHP Code: $sum = $_POST['one'] + $_POST['two']; I have 2 textboxes, one named "one", the other named "two". I want the page to also display the answer under the text boxes but it updates in real time when you input a value into the textboxes. How would I do that? I've got this code below, it seems ok, but sometimes, the multiplication does not work..i don't know why... pls help me..thanks btw, this is my 1st javascript "application", so, im really new into this thanks in advance for any help Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My First JavaScript Application</title> <style type="text/css"> td { text-align:center } table { background:#660033 } body { background-color:#666; text-wrap:supress; } h1 { color:#FFFFFF; } .ex { background-color:#99F; color:#660000; font:Verdana, Geneva, sans-serif; font-size:20px; text-align:right; } .ec { background-color:#CCC; color:#660033; font:Arial, Helvetica, sans-serif; font-size:18px; } </style> <script type="text/javascript"> var num1 = 0; var ope = 0; var num2 = 0; var ek = 0 function addOnScr1() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+1; document.getElementById("nums").value = txt; } function addOnScr2() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+2; document.getElementById("nums").value = txt; } function addOnScr3() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+3; document.getElementById("nums").value = txt; } function addOnScr4() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+4; document.getElementById("nums").value = txt; } function addOnScr5() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+5; document.getElementById("nums").value = txt; } function addOnScr6() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+6; document.getElementById("nums").value = txt; } function addOnScr7() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+7; document.getElementById("nums").value = txt; } function addOnScr8() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+8; document.getElementById("nums").value = txt; } function addOnScr9() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+9; document.getElementById("nums").value = txt; } function addOnScr0() { document.getElementById("neg").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+0; document.getElementById("nums").value = txt; } function addOnScrp() { document.getElementById("neg").disabled=true; document.getElementById("point").disabled=true; if((ek!=0)) { document.getElementById("nums").value = ""; ek = 0; } var txt = document.getElementById("nums").value; txt = txt+"."; document.getElementById("nums").value = txt; } function addOnScrn() { var txt = document.getElementById("nums").value; txt = txt+"-"; document.getElementById("nums").value = txt; } function add() { document.getElementById("neg").disabled=false; document.getElementById("point").disabled=false; ek=1; if(num1==0) { num1 = parseFloat(document.getElementById("nums").value); } else { ent(); } ope = 1; } function diff() { document.getElementById("neg").disabled=false; document.getElementById("point").disabled=false; ek=1; if(num1==0) { num1 = parseFloat(document.getElementById("nums").value); } else { ent(); } ope = 2; } function prod() { document.getElementById("neg").disabled=false; document.getElementById("point").disabled=false; ek=1; if(num1==0) { num1 = parseFloat(document.getElementById("nums").value); } else { ent(); } ope = 3; } function quo() { document.getElementById("neg").disabled=false; document.getElementById("point").disabled=false; ek=1; if(num1==0) { num1 = parseFloat(document.getElementById("nums").value); } else { ent(); } ope = 4; } function clr() { num1 = 0; num2 = 0; ope = 0; document.getElementById("nums").value = ""; document.getElementById("neg").disabled=false; document.getElementById("point").disabled=false; ek = 0; } function ent() { ek = 1; var ans; num2 = parseFloat(document.getElementById("nums").value); switch(ope) { case 1: ans = num1 + num2; document.getElementById("nums").value = ans; num1 = ans; break; case 2: ans = num1 - num2; document.getElementById("nums").value = ans; num1 = ans; break; case 3: ans = num1 * num2; document.getElementById("nums").value = ans; num1 = ans; break; case 4: ans = num1 / num2; document.getElementById("nums").value = ans; num1 = ans; break; default: document.getElementById("nums").value = "???"; } } function enterz() { document.getElementById("neg").disabled=false; document.getElementById("point").disabled=false; ek = 1; var ans; num2 = parseFloat(document.getElementById("nums").value); switch(ope) { case 1: ans = num1 + num2; document.getElementById("nums").value = ans; num1 = 0; break; case 2: ans = num1 - num2; document.getElementById("nums").value = ans; num1 = 0; break; case 3: ans = num1 * num2; document.getElementById("nums").value = ans; num1 = 0; break; case 4: ans = num1 / num2; document.getElementById("nums").value = ans; num1 = 0; break; default: document.getElementById("nums").value = "???"; } } </script> </head> <body><center> <form><h1> Calculator</h1> <table border="0"> <tr><th colspan="4"><input type="text" class = "ex" id = "nums" name = "nums" /></th></tr> <tr><td><button type="button" class = "ec" onclick="addOnScr1()">1</button></td> <td><button type="button" class = "ec" onclick="addOnScr2()">2</button></td> <td><button type="button" class = "ec" onclick="addOnScr3()">3</button></td> <td><button type="button" class = "ec" onclick="add()">+</button></td> </tr> <tr><td><button type="button" class = "ec" onclick="addOnScr4()">4</button></td> <td><button type="button" class = "ec" onclick="addOnScr5()">5</button></td> <td><button type="button" class = "ec" onclick="addOnScr6()">6</button></td> <td><button type="button" class = "ec" onclick="diff()">--</button></td></tr> <tr><td><button type="button" class = "ec" onclick="addOnScr7()">7</button></td> <td><button type="button" class = "ec" onclick="addOnScr8()">8</button></td> <td><button type="button" class = "ec" onclick="addOnScr9()">9</button></td> <td><button type="button" class = "ec" onclick="prod()">*</button></td></tr> <tr><td><button type="button" class = "ec" id="neg" onclick="addOnScrn()">-</button></td> <td><button type="button" class = "ec" onclick="addOnScr0()">0</button></td> <td><button type="button" class = "ec" id="point" onclick="addOnScrp()">.</button></td> <td><button type="button" class = "ec" onclick="quo()">/</button></td></tr> <tr><td></td> <td><button type="button" class = "ec" onclick="enterz()">=</button></td> <td><button type="button" class = "ec" onclick="clr()">C</button></td><td> </td></tr> </form> </body> </html> Hi everyone, I am currently doing a project with a calculator and I am having a bit of trouble trying to figure something out and was wondering if I could get some help with it or possibly even a good site to read up on it. I am trying to make it so my calculator is disabled on startup. I can only seem to get my textbox disabled but I need everything disabled except the ON button.. so whey they click the ON button everything is enabled. when they click the OFF button everything is wiped and disabled.. I'm not to familiar with that yet so all the help is greatly appreciated.. I'm not sure if I'm going on the right direction with this but heres my code... Code: var value function start() { form1.tb1.disabled = true; } onload = start; function enable() { form1.tb1.disabled = false } function shownumber(value){ document.form1.tb1.value=document.form1.tb1.value+value document.form1.plus.disabled=false document.form1.minus.disabled=false document.form1.times.disabled=false document.form1.divide.disabled=false } function showoperator(value){ document.form1.tb1.value=document.form1.tb1.value+value } function evalit(){ document.form1.tb1.value=eval(document.form1.tb1.value) } and my body is Code: <table width="210" border="1" bgcolor="#0000"> <form name="form1"> <tr> <td align="center"> <table width="173" border="0" cellspacing="0" cellpadding="0" height="130"> <tr> </tr> <tr> <td colspan="3"> <input type="text" name="tb1" size="20"> </td> <td width="53" height="0"> <font color="#FFFFFF"> <input type="button" name="clear" value=" c " onClick="document.form1.tb1.value=''"> </font></td> </tr> <tr> <td width="53" height="0"> <input type="button" name="7" value=" 7 " onClick="shownumber('7')"> </td> <td width="53" height="0"> <input type="button" name="eight" value=" 8 " onClick="shownumber('8')"> </td> <td width="53" height="0"> <input type="button" name="nine" value=" 9 " onClick="shownumber('9')"> </td> <td width="53" height="0"> <font color="#FFFFFF"> <input type="button" value=" / " name="divide" onClick="showoperator('/')"> </font></td> </tr> <tr> <td width="53" height="0"> <input type="button" name="four" value=" 4 " onClick="shownumber('4')"> </td> <td width="53" height="0"> <input type="button" name="five" value=" 5 " onClick="shownumber('5')"> </td> <td width="53" height="0"> <input type="button" name="six" value=" 6 " onClick="shownumber('6')"> </td> <td width="53" height="0"> <font color="#FFFFFF"> <input type="button" name="times" value=" x " onClick="showoperator('*')"> </font></td> </tr> <tr> <td width="53" height="0"> <input type="button" name="one" value=" 1 " onClick="shownumber('1')"> </td> <td width="53" height="0"> <input type="button" name="two" value=" 2 " onClick="shownumber('2')"> </td> <td width="54" height="0"> <input type="button" name="three" value=" 3 " onClick="shownumber('3')"> </td> <td width="72" height="0"> <font color="#FFFFFF"> <input type="button" name="minus" value=" - " onClick="showoperator('-')"> </font></td> </tr> <tr> <td width="53" height="0"> <input type="button" name="zero" value=" 0 " onClick="shownumber('0')"> </td> <td width="53" height="0"> </td> <td width="54" height="0"> <input type="button" value=" + " name="plus" onClick="showoperator('+')"> </td> <td width="72" height="0"> <font color="#FFFFFF"> <input type="button" name="calculate" value=" = " onClick="evalit()" </font></td> <tr> <td> <input type="button" id="but1" value="On" onClick="enable();"> <input type="button" id="button2" value="Off"> </td> </tr> </table> </td> </tr> </form> </body> </html> Hi there, me again. Need help with this bit of code. The script itself I got from the JS Source, and modified it myself a bit so that instead of the user inputting their DOB via a prompt box, it could be entered via dropdown lists. I attempted to do this, but have come across a problem. For some reason, the result always (or at least, that's what it looks like to me) appears to be the array index element of [4], i.e. Friday. Not really sure why this is, but no matter what date is entered out of the example dates shown below in the HTML, the result is always Friday. Double-checked on another similiar calculator script online, and sure enough, the results are wrong. So it's a problem with my script. Except I can't identify it. So any help from Old Pedant/Philip M, or whoever else on the forum has the patience to help me , would be much appreciated. Code: <script type="text/javascript" language="javascript"> function findDay() { var arr = new Array(); arr[0] = "Monday"; arr[1] = "Tuesday"; arr[2] = "Wednesday"; arr[3] = "Thursday"; arr[4] = "Friday"; arr[5] = "Saturday"; arr[6] = "Sunday"; var selectdate = document.getElementById("selectdate").selectedIndex; var selectmonth = document.getElementById("selectmonth").selectedIndex; var selectyear = document.getElementById("selectyear").selectedIndex; var birthday = new Date(selectdate + selectmonth + selectyear); var day = birthday.getDay(); document.getElementById("div1").innerHTML = "You were born on a " + arr[day] + "!" } </script> Code: <html> <h1>Day Of The Week?</h1> Input your date of birth, and click the Go button to find out what day of the week you were born on. <hr/> <form style="text-align: center;"> <select id="selectdate"> <option>1</option> <option>2</option> <option>3</option> </select> <select id="selectmonth"> <option>January</option> <option>February</option> <option>March</option> </select> <select id="selectyear"> <option>1995</option> <option>1996</option> <option>1997</option> </select> <br/> <br /> <input align="middle" type="button" value="Go" onclick="findDay()"> </form> <div id="div1"></div> </html> I am working on an Mpg calculator and I am stuck any input would be greatly appreciated. Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Gas Mileage</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <script type = "text/javascript"> /*<![CDATA[*/ function calcMPG() { var startMiles= document.forms[0].startingMileage.value; var endMiles= document.forms[0].endingMileage.value; var gallons=document.forms[0].gallons.value; if(isNaN(startMiles) isNaN(endMiles)) isNaN(gallons)) { window.alert("You must enter a number"); } else { if (gallons > 0) { document.forms[0].gallons.value= ((endMiles-startMiles)/gallons).toFixed(1); } } } /*]]>*/ </script> </head> <body> <script type = "text/javascript"> /*<![CDATA[*/ document.write("<p><h1>Miles Per Gallon Calculator</h2></p>"); document.write("<p><h3>You must enter your starting and ending mileage and gallons used.</h3></p>"); /*]]>*/ </script> <form action=""> <p> Starting Mileage<br /> <input type="text" name="startingMileage" value="0" onchange="calcMPG()" /> </p> <p> Ending Mileage <br /> <input type="text" name="endingMileage" value="0" onchange="calcMPG()" /></p> <p>Gallons Used<br /> <input type="text" name="gallons" value="0" onchange="calcMPG()"/></p> <p>Miles Per Gallon<br /> <input type="text" name="miles per gallon" value="0" /></p> </form> </body> </html> I have a mistake when it is suppose to calculate. Thanks to all I have been having problems adding checkboxes and option selects in the same function.. Here's what I have. Code: <script type="text/javascript"> function bonuscalc() { var aaaa = document.getElementById("aaaa").value; var bbbb = document.getElementById("bbbb").value; var cccc = document.getElementById("cccc").value; if (dddd.checked){ var dddd = document.dddd.value = 2; } else { var dddd = document.dddd.value = 1; } var eeee = document.getElementById("eeee").value; var bonus = aaaa* bbbb * dddd * cccc * eeee; var roundbonus = Math.round(bonus*10)/10; document.getElementById("roundbonus").value = roundbonus; } </script> Javascript first.. The part I'm not sure about is the if/else part for checbox. Now the html: Code: <form action="" id="calc1"> <table width="100%"> <tbody> <tr> <td>A status: <select id="aaaa"> <option value="1.1">A1</option> <option value="1.02">A2</option> <option value="1">A3</option> <option value="0.95">A4</option> </select> </td> <td>B status: <select id="bbbb"> <option value="1">0%</option> <option value="0.99">1%</option> <option value="0.98">2%</option> <option value="0.97">3%</option> <option value="0.96">4%</option> <option value="0.95">5%</option> <option value="0.94">6%</option> <option value="0.93">7%</option> <option value="0.92">8%</option> <option value="0.91">9%</option> <option value="0.90">10%</option> </select> </td> <td>E status:<input id="eeee" type="text" /> </td> <td>D status: <input id="dddd" name="dddd" type="checkbox"> </td> <td>C status: <select id="cccc"> <option value="1">No</option> <option value="1.2">Yes</option> </select> </td> </tr> <tr> <td width="100%"> <input type="button" value="Submit" onclick="bonuscalc()" /> <br /> Result: <input type="text" readonly="readonly" id="roundbonus" /> </td> </tr> </tbody> </table> </form> I hope it's not too confusing. The idea is to assign value 2 if the checkbox is checked and value 1 is it isn't.. |