Search code examples
javascripthtmlnestedinnerhtml

Nested if - JavaScript


I want the code to read the input value for Bz and give a greeting along with the answer from the equation. This is what I have this far. It's not giving a 'greeting' after the computation. Help?

  <html>
  <body>

<form id="function" onsubmit="return false" oninput="o.value = 3.21 + (9.63 * parseInt(Bz.valueAsNumber))">
    3.21+(9.63 *<input name="Bz" type="number" step="any">) =
   <output name="o" for="intial set Bz">0</output>&nbsp;nT

</form>

<div id="mys1"></div>

<script language="JavaScript">   
 function myMath() 
 {
    var Bz;
    var greeting; 
    if(Bz>-50)
    {
         if (Bz>-30)
         {
               greeting = "None"; 
         }
         else  
         {
               greeting = "Small ";
         }
         else 
         { 
               if (Bz> -80) 
               {
                     greeting = "Mild ";
               }
               else 
               {
                     greeting = "Large ";
               }
          }
     }
 document.getElementById("mys1").innerHTML= greeting;
 };  
</script>

</body>
</html>

Solution

  • I found your problem. Bz value is not being loaded. Seting input name attribute will not store the value into a variable. That attribute is for form submission.

    What you should do is:

    function myMath() 
     {
        var Bz = $("input[name='Bz']").val();
        ...
        ...
        ...
     }