> I would like to use Objects better, because I would like to use the
> little search code. When I try and run the program the error message I
> get from FF is 'Error: abbrs[somevar] is undefined'.
> The full script:
>
> $("#load_abbr_data").click(function()
> {
> var abbrs = {}
>
>         $.getJSON("jsondata.php",function(data)
>                 {
>
>                         $.each(data.abbr_data, function(i,get_abbr_data)
>                         {
>                                 var abbr = get_abbr_data.ABBR;
>                                 var desc = get_abbr_data.DESCRIPTION;
>                                 var lang =  get_abbr_data.LANG;
>
>                                 abbrs =
>                                 {
>                                         abbr : [desc],
>                                 }
>
>                         });
>
>                 });
>
>         var somevar = "PHP";
>         for( var i = 0 ; i < abbrs[somevar].length; i++)
>         {
>                 var expanded = abbrs[somevar];
>
>         }
>         document.write( somevar + " is short for " + expanded );
>
> });
>
>



Yeah, you're kind of all over the place with that code.  Try this
(note the comments):

$("#load_abbr_data").click(function()
{
    var abbrs = {}; // hash

    $.getJSON("jsondata.php",function(data)
    {
        $.each(data.abbr_data, function(i,get_abbr_data)
        {
            var abbr = get_abbr_data.ABBR;
            var desc = get_abbr_data.DESCRIPTION;
            var lang =  get_abbr_data.LANG;

            // add to hash
            abbrs[abbr] = desc;
        });

        // still inside getJSON callback, dump output
        // by iterating all properties of abbrs hash
        for (var a in abbrs)
        {
            // 'a' is the current property we're iterating
            document.write(a + " is short for " + abbrs[a]);
        }
    });
});

Reply via email to