Setting the value of an object through Jquery Each()

Friday, July 11, 2014

I'm attempting to iterate through an object.key (newPickup) and set the value of object.key equal to the value specified in a form. I can get through to the end without throwing any errors however, when i call the object all items are undefined. I set up a fiddle with it already loaded and I'm hoping one of you talented magnificos can let me know where I messed up. Thanks in advance.


Fiddle: http://ift.tt/1r3SOSJ



<button id="startPickup">Schedule Pickup</button>
<div id="pickupWrap"></div>


function Pickup(pickupDate, clinicName, phoneNumber, qtySpec, fedexConfirm){
this.pickupDate = pickupDate;
this.clinicName = clinicName;
this.phoneNumber = phoneNumber;
this.qtySpec = qtySpec;
this.fedexConfirm=fedexConfirm;
}
//The Pickup Object
var newPickup = new Pickup();

//Click the button to make the form
$("#startPickup").click(function(){
//Empty if exists
$("#pickupWrap").empty();
//Make the inputs
$.each(newPickup, function(key,value){
$('#pickupWrap').append("<label for=\""+key+"\">"+key+": </label><input type=\"text\" id=\""+key+"\" class=\"pickupInput\"><br>");
});
//Make the button
$('#pickupWrap').append("<button id='submitPickup'>Submit Pickup</button");

//Capture the form inputs from pickup
$("#submitPickup").click(function(){
console.log("click");

//Get Values and Indexes
$('.pickupInput').each(function(){
var pickupIndex = $(this).attr("id");
var pickupValue =$(this).val();

console.log(pickupIndex+" | "+ pickupValue);

//Set newPickup to appropriate value
$.each(newPickup, function(key, value){
if (key === pickupIndex) {
console.log("we have a match");
value = pickupValue;
};
})
});
console.log(newPickup);

});
});






http://ift.tt/1y4p2xS