Ajax request for autocomplete sends results back twice.
I am sending a ajax request for search results and when I console.log() the results it sends the result back twice. Here is my code.
Any help would be appreciated.
- //Ready the DOM
- $(function(){
-
- //Placeholder plugin for input text area
- $('input, textarea').placeholder();
-
- //bind a submit function to the form div
- $('form').bind('submit', function(e){ return false;});
-
- //variables for the input field, and search box div.
- var input = $('input').eq(0),
- search = $('#searchresults')
- ;
-
- //bind a keyup function to capture keystrokes
- input.bind('keyup', function(e){
- //variable that captures the values being put in the text field.
- var value = input.val();
- //fade in the search results box.
- search.fadeIn(500);
-
- //if statements for the up, down, esc, enter keystrokes.
- if(e.which === 38){
-
- }if(e.which === 40){
-
- }if(e.which === 13){
- search.find('li');
- }if(e.which === 27){
- search.fadeOut(500);
-
- //if statement that sends a ajax request when the keystrokes are 3 or more.
- }else if(value.length > 3){
- //the ajax request with options
- $.ajax({
- url: 'xhr/vidsearch.php',
- type:'GET',
- dataType:'json',
- data:{q:$(this).val()},
- //success function with to show the results of the ajax request.
- success: function(response){
- //if statement to show a 'no result' response to input value that does not match ajax request.
- if(response.length) {
- $.each(response, function (i, item) {
- var listItem = $('<li><a href="'+ item.url+'"><img src="images/icons-small/'+ item.icon +'" /><span>'+item.title+'</span></a></li>')
- .appendTo(search);
- })
- }
- }
-
- });
-
- }
- return false
-
- });
-
- //binds a focusout function to the input field when the user click outside the search box the result box fades out.
- input.bind('focusout', function(e){
- search.fadeOut(500);
- });
- });