guyfromfl
guyfromfl

Reputation: 1212

javascript Undefined variable/array

I have a small function that takes a dynamic list of textboxes with urls and sends it to the server. I am trying to load the urls into an array then pass them off in a JSON object. The problem is, when I get to that point in the code, I keep getting the error:

TypeError: url_data[this_url] is undefined
http://localhost/2011/admin/public/assets/js/tssol.ui.js
Line 196

Here is the block that returns that error:

$("#dialogEditor").dialog('option', 'buttons', {'Save' : function() {
    /* Define variables */
    var url_data = new Array();
    var this_url = 0;

    /* I've tried this with and without explicitly passing the vars... */
    $("#website_editor input").each(function(url_data, this_url) {
        var id =$(this).attr('name').match(/\d+/);
        var url=$(this).val();

        /* Test the varible each iteration */
        console.log("this_url: " + this_url + " id: " + id + " url: " + url);

    /* Line 196 */ url_data[this_url].id = id;
        url_data[this_url].url = url;

        this_url++;
});


data = JSON.stringify(url_data);
//data = url_data;
//console.log(data);

$.ajax({
    url: 'ajax.websites.php',
    dataType: 'json',
    data: {
        action: "update_resort",
        ResortId: resort,
        data: data
    }
});

$(this).dialog("close");
},
'Cancel': function(){$(this).dialog("close");
}});
},

Sorry about the formatting

Upvotes: 1

Views: 440

Answers (1)

Renato Gama
Renato Gama

Reputation: 16599

I would do this:

url_data[this_url] = {
    id: id,
    url: url
}

Upvotes: 3

Related Questions