2

I'm trying to use JSON data (returned from PHP) to create HTML for display to the client side.

I've got this in a function that is also serving to populate google maps.

function searchLocationsNear(center) {

 //some AJAX that's working fine//

success:function(data, response){
console.log(data);
var markerNodes = data;

  var bounds = new google.maps.LatLngBounds();

for (var i = 0; i < markerNodes.length; i++) {
var uid = markerNodes[i].id;
    var name = markerNodes[i].name;
    var address = markerNodes[i].address;
    var distance = parseFloat(markerNodes[i].distance);
    var avatar = markerNodes[i].avatar;
    var bio = markerNodes[i].bio;
    var rate = markerNodes[i].price;
    var latlng = new google.maps.LatLng(
        parseFloat(markerNodes[i].lat),
        parseFloat(markerNodes[i].lng));

    createOption(name, distance, i);
   createMarker(latlng, name, avatar, uid, bio, rate);
       bounds.extend(latlng);

     var html = "<div style='float:left; padding-right:10px;'><img src='user/"+uid+"/"+avatar+"'s alt='Tutor - "+name+" Profile Picture'></div><div class='infowindow'><h3>" + name + "</h3><hr><h4>Rate: "+rate+"</h4>"+bio+"</div>"; 
console.log(html);

            $('#thumblist').html(html);
console.log($('#thumblist'));
  }

This Google Map is populating fine, and the console.log($('#thumblist')); shows the two entries that I need to show, but only one of them is appended to #thumblist (the last one). Can anyone help?

2
  • did you set dataType : "json"? Commented Jun 19, 2013 at 15:49
  • @pvnarula - yes - and the strange thing is that the map is showing both objects, and the console.log for at the very start of the function also shows both of them, but only one goes into the html var Commented Jun 19, 2013 at 15:53

2 Answers 2

2

You're overwriting the #thumblist HTML on each iteration. Instead you should append:

 $('#thumblist').append(html);

.html() will overwrite the entire content of the element, whereas .append() will leave the current content intact and just add to the end.

Sign up to request clarification or add additional context in comments.

Comments

0

Are you using $('#thumblist').html(html); to assign the html to that element? If so, you are writing over the html in $('#thumblist') each time you assign your var html to it. Can you use append() or after() instead? That way you won't replace the html in thumblist.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.