Guest User

jQuery autocomplete

a guest
Mar 21st, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.56 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
  5. <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
  6. <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
  7. <style >
  8.     .thumb-img {
  9.         width: 25px;
  10.         margin-right: 5px;
  11.     }
  12. </style>
  13. <script type="text/javascript">
  14.  
  15.     $(document).ready(function() {
  16.         $("#project").keypress(function() {
  17.             //pega valor project
  18.             var searchField = $(this).val();
  19.        
  20.             //pega json    
  21.             $.getJSON('data.php?query='+searchField, function(json){
  22.            
  23.             //complete field
  24.             $("#project").autocomplete({
  25.               minLength: 0,
  26.               source: json,
  27.               focus: function( event, ui ) {
  28.                 $("#project").val( ui.item.label );
  29.                 return false;
  30.               }
  31.             });
  32.      
  33.             //renderisando com html + css
  34.             $("#project").data( "ui-autocomplete" )._renderItem = function( ul, item ) {
  35.                 var $li = $('<li>'),
  36.                     $img = $('<img class="thumb-img">');
  37.                     $img.attr({
  38.                       src: item.pic,
  39.                       alt: item.username,
  40.                     });
  41.                     $li.attr('data-value', item.username);
  42.                     $li.append('<a href="#">');
  43.                     $li.find('a').append($img).append(item.username);    
  44.                     return $li.appendTo(ul);
  45.                 };
  46.            
  47.             });
  48.    
  49.         });    
  50.        
  51.     });
  52.    
  53.  
  54.    
  55.     </script>
  56.  
  57.   <meta charset="utf-8">
  58.   <title>JS Bin</title>
  59. </head>
  60. <body>
  61.   <div id="project-label">Select a project (type "j" for a start):</div>
  62.   <input id="project" autocomplete="off">
  63. </body>
  64. </html>
Add Comment
Please, Sign In to add comment