Link to home
Start Free TrialLog in
Avatar of EICT
EICTFlag for United Kingdom of Great Britain and Northern Ireland

asked on

jQuery if statement to check variable and assign value to selector element accordingly

I'm quite new to jquery.
I have a simple jquery function which takes the value from two html select inputs ( #number1 and #number 2), multiples the values together and assigned a string value to the DIV element (#total) according to what the calculated value(total) is.

Everything works fine except the if statements. I'm not too sure how best to do this in jquery?


 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#number1, #number2").change(function(){
    var cons = $("#number1").val() || 0;
    var likely = $("#number2").val() || 0;
    
    var total = (cons * likely)
      
    if ((total == "0"))
     { var result = "N/A";}
    else if ((total >= "1") && (total <= "2"))
     { var result = "Low";}
    else if ((total >= "3") && (total <= "4"))
     { var result = "Medium";}
    else if ((total >= "6") && (total <= "9"))
     { var result = "High";}
    else if ((total >= "12") && (total <= "16"))
     { var result = "Immediate";}    
       
    
    $("#total").html(result);    
    });
});



</script>';

echo '<form><table class="my-table">
    <thead>
        <tr>
            <td>Type</td>
            <td>Consequence</td>
            <td>Likelyhood</td>
            <td>Total</td>
        </tr>
    </thead>
    <tbody>
        <tr data-row-num="1">
        <td>Risk Self</td>
        <td><select id="number1">
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
             </select>   
        </td>
              <td><select id="number2">
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
             </select>   
        </td>
        <td><div id="total" /></td>
        </tr>
   </tbody>
</table>
</form>
      ';  

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dorababu M
Dorababu M
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EICT

ASKER

Works like a treat. Thank you. Quick question   - are you using a switch condition here?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial