Wesley Kluckhohn
Wesley Kluckhohn

Reputation: 115

Javascript If statement not working properly

My math all seems correct, the only thing that's not working is the if statement. It always does the math off of the male value.

the javascript:

function updatespending() {
    var age = parseFloat(document.forms[0].CurrentAge.value);
    var value = parseFloat(document.forms[0].NetWorth.value);

    if(document.forms[0].sex.value = "male") {
        var yearsleft = Math.round (76 - age);
    } else if(document.forms[0].sex.value = "female") {
        var yearsleft = 81 - age;
    }
}

the html:

<table width="433" border="1" align="center">
    <tr>
        <td class="liquidate">How much will you have to live off of if you liquidate all of your assets?</td>
    </tr>
    <tr>
        <td width="381" height="68">
            <table width="100%" border="0" cellpadding="4" cellspacing="4">
                <tr>
                    <td align="right">Sex:</td>
                    <td>
                        <input type="radio" name="sex" value="male">Male<br>
                        <input type="radio" name="sex" value="female">Female
                    </td>
                </tr>
                <tr>
                    <td align="right">Current Age:</td>
                    <td>
                        <input type="text" onchange="updatespending()" onkeyup="numericOnly(this)" maxlength="2" size="3" value="0" name="CurrentAge" />
                    </td>
                </tr>
                <tr>
                    <td align="right" >Annual Spending Available:</td>
                    <td>$<input name="AnnualSpending" disabled="disabled" readonly /> </td>
                </tr>
            </table>
        </td>
    </tr>
</table>

I would also like to make it so that if the individual changes the sex from male to female that it would updatespending as it does when I update the current age. Not so sure that a on change would work correctly. Any other suggestions would be great.

Upvotes: 1

Views: 2898

Answers (3)

Marcos Sant&#39;Anna
Marcos Sant&#39;Anna

Reputation: 66

Your are using only one equal, always remember... a single equal is assignment of value, and will almost always return true, double equals is a comparison, and triple equals a type strict comparison.

That being said, try changing it to:

if(document.forms[0].sex.value == "male") {
   var yearsleft = Math.round (76 - age);
} else if(document.forms[0].sex.value == "female") {
   var yearsleft = 81 - age;
}

Let us know if that helps.

Upvotes: 0

LFF
LFF

Reputation: 228

I see some errors.

  1. the input radio is not closed. It should be like

    <input type="radio" name="sex" value="male"></input>
    
  2. You did not provide a default value for the radio. So after the page is loaded, NO value is checked. As a result, the document.forms[0].sex.value is neither male nor female. Please add a default value and try again:

    <input type="radio" name="sex" value="male" checked="checked">Male</input>
    <input type="radio" name="sex" value="female">Female</input>
    
  3. the = should be == , in the if statement.

Upvotes: -2

Mritunjay
Mritunjay

Reputation: 25892

Problem is you are saying

if(document.forms[0].sex.value = "male")
//this statement always return true so it will always go to if statement block.

You should say

if(document.forms[0].sex.value == "male") {

Upvotes: 7

Related Questions