Shina
Shina

Reputation: 2066

jQuery / json loop not giving me full result with .html()

// Calling the video function with JSON

$.getJSON("videos.php", function(data){
// first check if there is a member available to display,
//if not then show error message
    if(data == '') { 
        $('#tabs-4').html("<div class='errorMember'>Sorry, there is currently no member available videos</div>");
    } 
    // if there is a member, then loop through each data available
    else {
          $.each(data, function(i,name){
            content = '<div class="left"><img src="' + name.pic + '"/>';
            content += '<p>' + name.name + '</p>';
            content += '<a href="' + name.link + '" target="_blank">Video link</a>';
            content += '</div><br/><hr>';
            $("#tabs-4").html(content);
        });
    }
});​

The problem is that it only gives me one result instead of list of results from the array but if I appendTo(content) .. it adds the full list of results under the current which is not what I want because I need to refresh that content with updated data.

Any ideas to what I'm doing wrong?

Upvotes: 0

Views: 157

Answers (3)

Rosmarine Popcorn
Rosmarine Popcorn

Reputation: 10967

Probably so far only the last Element is getting Displayed .

// Calling the video function with JSON

$.getJSON("videos.php", function(data){

    // first check if there is a member available to display, if not then show error message
    if(data == '') { 
        $('#tabs-4').html("<div class='errorMember'>Sorry, there is currently no member available videos</div>");
    } 
    // if there is a member, then loop through each data available
    else {
        //If you want to Clear the Container html
        $("#tabs-4").html(''); 
        $.each(data, function(i,name){
            content = '<div class="left"><img src="' + name.pic + '"/>';
            content += '<p>' + name.name + '</p>';
            content += '<a href="' + name.link + '" target="_blank">Video link</a>';
            content += '</div><br/><hr>';
            $("#tabs-4").append(content);
        });
    }
});

Upvotes: 1

Guffa
Guffa

Reputation: 700232

Empty the element before filling it:

$('#tabs-4').empty();
$.each(data, function(i,name){
  var content =
    '<div class="left"><img src="' + name.pic + '"/>' +
    '<p>' + name.name + '</p>' +
    '<a href="' + name.link + '" target="_blank">Video link</a>' +
    '</div><br/><hr>';
  $("#tabs-4").append(content);
});

Or put all the elements in the string before putting it in the element:

var content = '';
$.each(data, function(i,name){
  content +=
    '<div class="left"><img src="' + name.pic + '"/>' +
    '<p>' + name.name + '</p>' +
    '<a href="' + name.link + '" target="_blank">Video link</a>' +
    '</div><br/><hr>';
});
$("#tabs-4").html(content);

Upvotes: 0

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123377

If I well understood, probably you may want do something like this

...
else {
        $("#tabs-4").empty(); // remove previous data (if any)

        $.each(data, function(i,name){
            content = '<div class="left"><img src="' + name.pic + '"/>';
            content += '<p>' + name.name + '</p>';
            content += '<a href="' + name.link + '" target="_blank">Video link</a>';
            content += '</div><br/><hr>';

            $("#tabs-4").append(content); // append new data
        });
}

Upvotes: 0

Related Questions