views:

25

answers:

2

Hi All,

This has got me for some reason the code below is translating the currency variable to a string and then not getting the actual value from the self named variable...

$("#quantity").change(function() {
    var euroPrice = new Array("12.95", "14.95", "18.95", "21.95");
    var gbpPrice = new Array("10.95", "11.95", "16.95", "18.95");

    var currency = $("#currency option:selected").val();
    ​​var currently = $("#quantity option:selected").val();

    $("#Price").text( currency[currently]);
    $("#amount").attr("value", currency[currently]);
});

Where #currency and #quantity are two select boxes in the HTML that I need the code to evaluate their currently selected values on changes, retrieving the correct value from the correct variable.

Any help greatly received :)

m

A: 

I am not sure what you are trying to achieve here exactly, but the complete lack of reference to euroPrice and gbpPrice looks very much like you are simply querying the wrong array for the value.

var currency = $("#currency option:selected").val();
​​var currently = $("#quantity option:selected").val();

$("#Price").text( currency[currently]);
$("#amount").attr("value", currency[currently]);

According to this code, currency will be a single value, namely the value attribute of the selected item in your currency selector, while currently will be also a single value, namely the value of the selected item of your quantity select box.

In the second pair of rows you are trying to reference currency[currently] which is trying to access a single string as an array. I think this is where you made the mistake. Isn't it possible that you wanted to have something like

$("#Price").text( euroPrice[currently]);
$("#amount").attr("value", euroPrice[currently]);

instead (in case you want the euro equivalent of the currency)?

CodeTwice
CodeTwice, yes but I need to pull euroPrice from the value of the select box "#currency" as this holds the two options - 'gbpPrice' and 'euroPrice'. But the script passes the concatenation as a string and doesn't evaluate the result...
mmuller
+1  A: 

I can't see the HTML...but the only thing that makes sense to me as to why you aren't using the two defined arrays is if currency is a dropdown with 2 potential values 'euroPrice' and 'gbpPrice' and you are trying to use this as a dynamic variable with quantity being the index of the array for either currency. If I'm correct in my guess, then I think what you are missing is an eval() statement. Also, note, that for selects you should not need to use the option:selected in jquery. The val() statement figures this out. I'm also assuming #amount is an input and doesn't need the attr()...so:

var currency = $("#currency").val();
var quantity = $("#quantity").val();

eval("var price = "+currency+"["+quantity+"];");
$("#Price").text( price );
$("#amount").val( price );

Hope that's what you were actually aiming at. If not, I think seeing the two HTML selects with at least the first few selectable values in each would be greatly beneficial in figuring out what you're trying to do.

Kevin Nelson
Tada !! so thats how to evaluate a set of variables - should have known its that same as PHP !Perfect answer. Thanks
mmuller