I’m trying to concatenate and return all 94 results into #title1.
All of the code below is working correctly in terms of displaying the information when ran, but rather than having 94 lines for each country returned when the user clicks the button. Im trying to get it to loop on the countryName tag and add to an array of results that is then pushed to #title1 once the looping comes to an end at country 94.
Any help?
$('#PostalCodeCountryInfoBtn').click(function() {
$.ajax({
url: "libs/php/getPostalCodeCountryInfo.php",
type: 'GET',
dataType: 'json',
success: function(result) {
console.log(JSON.stringify(result));
if (result.status.name == "ok") {
$('#title1').html(result['data'][0]['countryName'] + " " + result['data'][1]['countryName']);
$('#title2').html(result['data'][1]['countryName']);
$('#title3').html(result['data'][2]['countryName']);
$('#title4').html(result['data'][3]['countryName']);
$('#title5').html(result['data'][4]['countryName']);
$('#title6').html(result['data'][94]['countryName']);
}
},
error: function(jqXHR, textStatus, errorThrown) {
// error code
}
});
});
2
Answers
You can try something like this
This will loop through the result.data array and select the country name per and appends it to title 1
If you want it all one line it is a simple map and join.
If you have 94 different elements you can loop over the elements (using a common class)