I'm just getting my feet wet with jQuery after beginning to read Sitepoint's "Novice to Ninja" but, as always, I'm left wondering if there's a better way to write the code I've come up with. As it turns out, the answer is almost always and emphatic "yes."
All these "if" statements seem ridiculous. How can I do this better? What functions should I look at to clean this up. Thanks for the help.
$('#user').change(function(){
var user_id = $('#user').val();
$.ajax({
type: 'POST',
url: '../admin/billing/' + user_id,
dataType: 'json',
success: function(billing){
//alert(billing.id);
var name = '<a href="../user/view/' + user_id +'">' + billing.fname + ' ' + billing.lname + '</a><br />';
if(billing.company_name != ''){
var company_name = billing.company_name + '<br />';
}else{
var company_name = '';
};
if(billing.address_one != ''){
var address_one = billing.address_one + '<br />';
}else{
var address_one = '';
};
if(billing.address_two != ''){
var address_two = billing.address_two + '<br />';
}else{
var address_two = '';
};
var csz = billing.city + ', ' + billing.state + ' ' + billing.zip + '<br />';
if(billing.phone != ''){
var phone = billing.phone + '<br />';
}else{
var phone = '';
};
var data = name + company_name + address_one + address_two + csz + phone;
$('#billing').empty().append(data);
$('input:text').val('');
$('#same-as-billing').attr('checked', false);
}
});
});