views:

18

answers:

1

Hi

i have this method:

set_period_help_text: function(periodInput){
        var metric = MonitorMetric.getSelectedMetric();
        var periodStr = $('select[name=metric_period]').val();
        var datapoints = Number(periodInput.attr("value"));
        var period = datapoints * periodLookup[periodStr][0];
}

var periodLookup = {
    "OneMinute": [1, "minute"],
    "FiveMinute": [5, "minute"],
    "OneHour": [1, "hour"],
    "OneDay": [1, "day"],
    "OneWeek": [1, "week"]
};

where var periodStr is being retrived from a dropdown menue that only has the values:

    "OneMinute"
    "FiveMinute" 
    "OneHour"
    "OneDay"
    "OneWeek"

i get a java script error when the page loads:

periodLookup[periodStr] is undefined [Break on this error] var period = datapoints * periodLookup[periodStr][0];

+3  A: 

what is periodStr's value? when it throws? i bet it doesn't contain what you think. possible reasons are multiple, like you're running the code before the DOM is ready.

just somebody
i think it was the fact it was running code while the value it was trying to retrieve was still not loaded.
Mo