0

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?

3
  • do you have element with id from and to ? Commented Apr 3, 2011 at 2:16
  • O, boy pure javascript is so painful :O Commented Apr 3, 2011 at 2:17
  • They are all at value and the values of from and to should be the combination of txt + The selected value Commented Apr 3, 2011 at 13:02

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.