0

I have a JS function that uses ajax to filter options in a sub navigation/filter menu.

It makes the calls correctly and receives the JSON response from the server, however it won't loop through the results.

It is only displaying the last result in the JSON response.

The ajax script:

function updateVenues(opts){
    $.ajax({
      type: "POST",
      url: "/venue-search/ajax/",
      dataType : 'json',
      cache: false,
      data: {filterOpts: opts, get_param: 'id'},
      success: function(records){
        $.each(records, function(idx, record){
         $("#searchResults").html('<a href="/venue-preview/' + record.id + '" class="various fancybox.ajax">' + record.title + "</a>");
       });
      }
    });
  }

A typical response from the server

[
{"id":"1","title":"Wedding Venue 1","main_image":"wedding-venue-1.jpg"},
{"id":"2","title":"Wedding Venue 2","main_image":"wedding-venue-2.jpg"},
{"id":"3","title":"Wedding Venue 3","main_image":"wedding-venue-3.jpg"}
]

Could anyone shed some light on why it isn't looping?

1
  • 3
    You aren't adding to #searchResults you are overwriting the contents of it every time you go through the loop. Try using .append() instead. Commented Aug 28, 2014 at 11:02

3 Answers 3

1

Each time you call .html it's overwriting the entire contents with the new value, which is why you only get the last one displayed. You need to use append:

function updateVenues(opts){
    $.ajax({
      type: "POST",
      url: "/venue-search/ajax/",
      dataType : 'json',
      cache: false,
      data: {filterOpts: opts, get_param: 'id'},
      success: function(records){
        //clear out any previous results first
        $("#searchResults").empty();
        $.each(records, function(idx, record){
         //now append each one
         $("#searchResults").append('<a href="/venue-preview/' + record.id + '" class="various fancybox.ajax">' + record.title + "</a>");
       });
      }
    });
  }
Sign up to request clarification or add additional context in comments.

2 Comments

You Sir, are a genius! Your answer is near enough identical to that of Shail, however you also include the "clearance" of the previous results. Worked perfectly. I shall mark it as the answer as soon as I can (9 minutes!). Thanks again!
I prefer $("#searchResults").empty(); over $("#searchResults").html('');
0

Try this

function updateVenues(opts){
$.ajax({
  type: "POST",
  url: "/venue-search/ajax/",
  dataType : 'json',
  cache: false,
  data: {filterOpts: opts, get_param: 'id'},
  success: function(records){
    $.each(records, function(idx, record){
     $("#searchResults").append('<a href="/venue-preview/' + record.id + '" class="various fancybox.ajax">' + record.title + "</a>");
   });
  }
});
  }

Comments

0

You should use:

$("#searchResults").append('<a href="/venue-preview/' + record.id + '" class="various fancybox.ajax">' + record.title + "</a>");

Otherwise you overwrite your "#searchResults") in each loop.

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.