JavaScript html forms calculator not working -


im trying make simple perimeter calculator projector screen.

the code should take response radio button input , diagonal length calculate perimeter accordingly.

here code atm:

<html>      <body>         <script language="javascript">             function calc() {                 var form = document.forms.calculator;                 var = number(getselectedvalue(form.elements.a));                 var b = number(getselectedvalue(form.elements.b));                   if (a = 0) {                     l = 0.8;                     h = 0.6;                 } else if (a = 1) {                     l = 0.872;                     h = 0.49;                 } else {                     l = 0.922;                     h = 0.386;                 }                  form.elements.total.value = 2 * b * (l + h);              }              function getselectedvalue(flds) {                 var = 0;                 var len = flds.length;                  while (i < len) {                     if (flds[i].checked) {                         return flds[i].value;                     }                     i++;                 }                  return "";             }         </script>         <title>calculator</title>   <pre>     <form name="calculator">         <label>a</label>              4:3: <input name="a" type="radio" onchange="calc()" value="0" checked>             16:9: <input name="a" type="radio" onchange="calc()" value="1">             2.39:1: <input name="a" type="radio" onchange="calc()" value="2">         <label>b</label>             screen size: <input name="b" type="text" onchange="calc()" checked>         <label>total</label>              <input type="text" name="total" onchange="calc()" readonly size="10"><br>          <input type="submit" value="submit calculation"> <input type="reset" value="clear">      </form>     </pre>      </body>  </html> 

your function getselectedvalue wrong. loops trough array of elements see 1 checked. trying same b 1 element not going trough loop , returns ''.

so instead of

var b = number(getselectedvalue(form.elements.b)); 

try

var b = number(form.elements.b.value); 

also, if else statements wrong. should == instead of =:

if (a == 0) {     l = 0.8;     h = 0.6; } else if (a == 1) {     l = 0.872;     h = 0.49; } else {     l = 0.922;     h = 0.386; } 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -