Nathan Campos
Nathan Campos

Reputation: 29497

Problem With Variables And Objects

I have 3 TextFields, called txtUSD, txtEUR, txtAUS. And a PopupList with the same values, minus the txt part, but I need to form the names of the TextFields to use based on the selection that the user made. So I've done this:

function btConvert_Click(event)
{
    var amount = document.getElementById("txtAmount").value;
    var rates = document.getElementById("lstConvertTo").value;
    var from = "txt" + document.getElementById("lstFrom").options[document.getElementById('lstFrom').selectedIndex].text;
    var to = "txt" + document.getElementById("lstConvertTo").options[document.getElementById("lstConvertTo").selectedIndex].text;
    var curr_from = document.getElementById(from).value;
    var curr_to = document.getElementById(to).value;

    if(curr_from > curr_to)
    {
        amount * rates;
    } else {
        amount / rates;
    }
    alert(result);
}

But every time I try it I get this error:

mobile/main.js line 215: Result of expression 'document.getElementById(from)' [null] is not an object.

How should I make it?

Upvotes: 0

Views: 122

Answers (1)

scribu
scribu

Reputation: 3078

From the error you're getting, it looks like there's a bug when generating the from variable.

You should consider storing document.getElementById('lstFrom') into it's own var, for brevity.

Upvotes: 1

Related Questions