-1

I have JQuery code on a web page that pulls data from a php function in JSON format. The code executes only after I click the button twice. In the chrome dev tools I see this error for the first button push: Uncaught TypeError: Cannot read property 'number' of undefined

// JSON Data example:
{
    "number": "555",
    "street": "S La Veta Park Cir",
    "propertynumber": "",
    "city": "Orange",
    "state": "CA",
    "zip": "92868"
}

// Script

var address;
var propertyinfo;
$('.step1').siblings().hide(); // hide all except step 1

$('.search').click(function(){
    var filledaddress = $('#address').val();
    $.get("addresslibrary.php?command=parse&address="+filledaddress, function(addressdata){
        address = JSON.parse(addressdata);
    });
    $('#propertyTitle').html(address.number+' '+address.street+' '+address.propertynumber+'<br>'+address.city+' '+address.state+' '+address.zip);

    $(this).closest('.step').hide().next('.step').show();
    return false;
});
$('.back').click(function(){
    $(this).closest('.step').hide().prev('.step').show();
    return false;
});
Salman A
  • 262,204
  • 82
  • 430
  • 521
dcoder50
  • 17
  • 7
  • `address = JSON.parse(addressdata);` isn't executed right after `$.get(...`, you should try to learn more about _asynchronous_ – Volune Sep 06 '14 at 10:16

1 Answers1

1

Set the html inside the callback function

$.get("addresslibrary.php?command=parse&address="+filledaddress, function(addressdata) {
  address = JSON.parse(addressdata); 
  $('#propertyTitle').html(address.number+' '+address.street+' '+address.propertynumber+'<br>'+address.city+' '+address.state+' '+address.zip);
});