Advertisement
arijulianto

HTML5 Datalist via AJAX

Apr 1st, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 0.83 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML5 Datalist via AJAX</title>
  5. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  6. <script>
  7. $(function() {
  8.     $("#search").on("input", function(e) {
  9.         var val = $(this).val();
  10.         if(val === "") return;
  11.         $.get("getData.php?field=title", {term:val}, function(res) {
  12.             var dataList = $("#searchBook");
  13.             dataList.empty();
  14.             if(res.DATA.length) {
  15.                 for(var i=0, len=res.DATA.length; i<len; i++) {
  16.                     var opt = $("<option></option>").attr("value", res.DATA[i][0]);
  17.                     dataList.append(opt);
  18.                 }
  19.  
  20.             }
  21.         },"json");
  22.     });
  23.  
  24. })
  25. </script>  
  26. </head>
  27.  
  28. <body>
  29.  
  30. <p>
  31.     <input type="text" name="search" id="search" placeholder="Type Something" list="searchBook" autocomplete="off">
  32.     <datalist id="searchBook"></datalist>
  33. </p>
  34.  
  35. </body>
  36. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement