I am trying to attach some data to options in the select menu of a specific class using jquery data() , which come from a jquery ajax() call. But I get an error that it is not attached when I try to call it. Or, more precisely, that it does not apply at all, since console.log($(this).data('address'.vendorName)); in the anti-penal line of code below gives the error Uncaught TypeError: Cannot read property 'vendorName' of undefined
The inner loop of $.each() is an attempt at the last hit because I thought that maybe just using $('.vendor_address_id_' + id).data.... did not apply to all elements of this class. But I do not think it is necessary. What am I doing wrong?
$.each(returnedData, function (key, val) { var id = val.id; //attach address information to each select option for display in .vendor_full_address_table $('.vendor_address_id_' + id).each(function (k, v) { $(this).data('address', { 'vendorName': val.vendor_name, 'address1': val.address1, 'address2': val.address2, 'city': val.city, 'state': val.state, 'zip': val.zip }); //gives error: Uncaught TypeError: Cannot read property 'vendorName' of undefined console.log($(this).data('address').vendorName); }); }); //console.log(returnedData); Object address_0: Object address1: "street address1" address2: "" city: "Kalamazoo" id: "15" state: "MI" vendor_name: "companyA" zip: "123456" address_1: Object etc... etc... etc... address_2: Object etc... etc... etc...
source share