1

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

2
  • What are you trying to obtain? this_url is the input element in .each, so you cannot use that as a key in the array. Commented Oct 17, 2012 at 18:00
  • If url_data[this_url] does not exist yet, you cannot set the id property on it... you have to assign an object to url_data[this_url] first. Commented Oct 17, 2012 at 18:10

1 Answer 1

3

I would do this:

url_data[this_url] = {
    id: id,
    url: url
}
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.