1

I have implemented auto complete search in my site. The problem is that response is coming properly but it do not iterate all elements in <ul><li> elements. Always shows one element in HTML but coming multiple from response.

On the top search see if you type only river it comes with river records but do not show all in one ul li means li iteration is not working.

Here is my Jquery code:

<script>    
$(document).ready(function(){
    $('#keyword').keyup(function() {
        var total;
        $.ajax({
            type: "POST",
            url: "http://realtyexecutivesny.com/getRecSet.php?rnd=" + Math.random(),
            data:{ key: $(this).val() },
            success: function(data){
                $.each(data.locations, function( key, value ) {
                    total =  '<li>'+data.locations[0].value+'</li>';
                });
               $('#lists').html('<ul>'+total+'</ul>'); 
            }
        });
    });
    $('#lists ul li').click(function(){
        $('#keyword').val($(this).html());
        $('#field').val($(this).parent().data('field'));
    });
    });
</script>

Here is HTML Code:

<input type="text" name="keyword" id="keyword" />
<input type="hidden" name="field" id="field" value="all" />
<div id="lists"></div>
Keep Coding
  • 636
  • 9
  • 26

3 Answers3

2

first Initialize total like

var total = ''; 

And in your each, Use index to get all records.

$.each(data.locations, function( index ) {
     total +=  '<li>' + data.locations[index].value + '</li>';
});

$('#lists').html('<ul>' + total + '</ul>'); 
Amit Soni
  • 1,437
  • 1
  • 9
  • 18
  • `locations: [{value: "Wading River, NY", altValue: null, display: "Wading River, NY", type: "city",…},…] 0: {value: "Wading River, NY", altValue: null, display: "Wading River, NY", type: "city",…} 1: {value: "Riverhead, NY", altValue: null, display: "Riverhead, NY", type: "city", propertyIndex: "",…} 2: {value: "Wading River, NY", altValue: null, display: "Wading River, NY", type: "city",…} 3: {value: "Wading River, NY", altValue: null, display: "Wading River, NY", type: "city",…} 4: {value: "Riverhead, NY", altValue: null, display: "Riverhead, NY", type: "city", propertyIndex: "",…}` – Keep Coding Mar 17 '15 at 07:52
  • and my listing shows only one element `
    • ` that is `Wading River, NY` when it should show all.
    – Keep Coding Mar 17 '15 at 07:53
  • let me try and get back to you. – Keep Coding Mar 17 '15 at 08:05
  • No, now it is not showing anything in response. See on website. – Keep Coding Mar 17 '15 at 08:06
  • @Testing : because you commented out this line `$('#lists').html('
      '+total+'
    '); `
    – Amit Soni Mar 17 '15 at 08:09
1

Try using .append() it's a more clean approach.

success: function (data) {
    $('#lists').html('<ul></ul>') // initiate clear element
    $.each(data.locations, function (key, value) {
        $('#lists ul').append('<li>' + data.locations[key].value + '</li>'); // append new elements to the ul element
    });
}

Also check this: jquery .html() vs .append()

Community
  • 1
  • 1
Piotr Dajlido
  • 1,982
  • 15
  • 28
-1

replace this:

total =  '<li>'+data.locations[0].value+'</li>';

with this:

total +=  '<li>'+data.locations[0].value+'</li>';

and befor the each loop define total like below:

var total = '';
$.each(data.locations, function( key, value ) {
    total +=  '<li>'+data.locations[0].value+'</li>';
});