Last active
June 26, 2016 10:15
-
-
Save sharif2008/5302b2b6a529b4b54d53312e30bccfbe to your computer and use it in GitHub Desktop.
JQuery Auto-Complete on Complex Javascript Object/JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> | |
<script src="//code.jquery.com/jquery-1.10.2.js"></script> | |
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input id="name" type="text"/> | |
<input id="email" type="text"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
var users = [{ | |
"email": "[email protected]", | |
"name": "marie" | |
}, { | |
"email": "[email protected]", | |
"name": "miss" | |
}]; | |
function handleAutocomplete(term) { | |
// use 'term' for custom filtering etc. | |
var options = $.grep(users, function(e) { | |
return e.name.startsWith(term); | |
}); | |
return options; | |
} | |
$("#name").autocomplete({ | |
minLength: 0, | |
source: function(request, response) { | |
var name = request.term; | |
var data = handleAutocomplete(name); /* get answers from somewhere.. */ | |
response(data); | |
}, | |
focus: function(event, ui) { | |
$("#name").val(ui.item.name); | |
return false; | |
}, | |
select: function(event, ui) { | |
$("#name").val(ui.item.name); | |
$("#email").html(ui.item.email); | |
return false; | |
} | |
}).autocomplete("instance")._renderItem = function(ul, item) { | |
return $("<li>") | |
.append("<a>" + item.name + "<br>" + item.email + "</a>") | |
.appendTo(ul); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have a look on live https://jsfiddle.net/duoc5bbh/1/