Displaying javascript calculation based on select menu option -


this 1 ought slam dunk somebody. total javascript novice, so i'm amazed i've gotten far. so when @ code, you'll see glaring problems , laugh @ me, because blindly guessing @ syntax @ point. please see offending code below , enjoy chuckle. i'll wait.

okay, i'm trying make simple calculator displays monthly savings of buying bus pass on paying cash. i'm trying display different answer depending upon zone chosen in select menu. formulas in each if statement work when list variables outside of if statements (hence aforementioned amazement), displaying each answer in series of alert boxes, i'd display applicable answer, , syntax out of whack.

i'd honored , humbled schooled in matter, bearing in mind of course less-than-rudimentary js skills. advance thanks.

<!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>cost savings calculator</title>    <script language="javascript">  <!-- begin ce mp savings calc script function  domath3() { var 1 = eval(document.theform3.elements[0].value) var 2 = eval(document.theform3.elements[1].value) var 3 = eval(document.theform3.elements[3].value)  if(document.theform3.elements[3].value = "z4"){ var prodz4 = (((one  *   two) * 4.25) *12) - 1680 alert("your yearly savings if buy commuter express zone 4 monthly pass $"  +  prodz4  +  ".") }  if(document.theform3.elements[3].value = "z3"){ var prodz3 = (((one  *   two) * 3.75) *12) - 1488 alert("your yearly savings if buy commuter express zone 3 monthly pass $"  +  prodz3  +  ".") }  if(document.theform3.elements[3].value = "z2"){ var prodz2 = (((one  *   two) * 3) *12) - 1200 alert("your yearly savings if buy commuter express zone 2 monthly pass $"  +  prodz2  +  ".") }  if(document.theform3.elements[3].value = "z1"){ var prodz1 = (((one  *   two) * 2.5) *12) - 960 alert("your yearly savings if buy commuter express zone 1 monthly pass $"  +  prodz1  +  ".") }  if(document.theform3.elements[3].value = "base"){ var prodbase = (((one  *   two) * 1.5) *12) - 684 alert("your yearly savings if buy commuter express base monthly pass $"  +  prodbase  +  ".") }  } // end ce mp savings calc script --> </script>    </head>  <body>  <form name="theform3"> days commute monthly: <input type="text"> daily trips on commuter express: <input type="text"> choose zone: <select name="zone"> <option value="base">base</option> <option value="z1">zone 1</option> <option value="z2">zone 2</option> <option value="z3">zone 3</option> <option value="z4">zone 4</option> </select> <input type="button" value="show result" onclick="domath3()">  </form>   </body> </html> 

here's 1 way of doing it:

function  domath3() {     var 1 = parseint(document.theform3.elements[0].value);     var 2 = parseint(document.theform3.elements[1].value);     var selection = document.getelementsbyname("zone")[0].value;      if(selection === "z4"){         var prodz4 = (((one  *   two) * 4.25) *12) - 1680;         alert("your yearly savings if buy commuter express zone 4 monthly pass $"  +  prodz4  +  ".");     }     else if(selection === "z3"){         var prodz3 = (((one  *   two) * 3.75) *12) - 1488;         alert("your yearly savings if buy commuter express zone 3 monthly pass $"  +  prodz3  +  ".");     }     else if(selection === "z2"){         var prodz2 = (((one  *   two) * 3) *12) - 1200;         alert("your yearly savings if buy commuter express zone 2 monthly pass $"  +  prodz2  +  ".");     }     else if(selection === "z1"){         var prodz1 = (((one  *   two) * 2.5) *12) - 960;         alert("your yearly savings if buy commuter express zone 1 monthly pass $"  +  prodz1  +  ".");     }     else if(selection === "base"){         var prodbase = (((one  *   two) * 1.5) *12) - 684;         alert("your yearly savings if buy commuter express base monthly pass $"  +  prodbase  +  ".");     } } 

fixes:

  1. you're missing ; (semi-colon's) everywhere - might seem 'optional' developer internally, javascript add them automatically leading hard track bugs. put 1 @ end of every statement.

  2. as mentioned elsewhere, don't use eval(), use parseint() integers , parsedouble() floats.

  3. = assignment operator, == , === used check equality. of these two, === should preferred. == perform coercion while === not.

  4. the biggest problem think document.theform3.elements[3].value gets button , not select box. should index 2.

optionally:

  1. you use else ifs since 1 of conditions true @ time. use switch-case instead.

  2. i avoid using indexes elements since might decide change form later , it's going painful make sure update indices correctly. check out getelementsbyid instead. @frits notes in comment below, use syntax document.formname.inputname (i.e. document.theform3.zone.value; form) element more cleanly , safely.

here's a working example.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -