Guest User

Untitled

a guest
Dec 16th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. $(function() {
  2. function split( val ) {
  3. return val.split( /,\s*/ );
  4. }
  5. function extractLast( term ) {
  6. return split( termย ).pop();
  7. }
  8.  
  9. $( "#friends" )
  10. // don't navigate away from the field on tab when selecting an item
  11. .bind( "keydown", function( event ) {
  12. if ( event.keyCode === $.ui.keyCode.TAB &&
  13. $( this ).data( "autocomplete" ).menu.active ) {
  14. event.preventDefault();
  15. }
  16. })
  17. .autocomplete({
  18. source: function( request, response ) {
  19. $.getJSON( "assets/functions/search.php", {
  20. term: extractLast( request.term )
  21. }, response );
  22. },
  23. search: function() {
  24. // custom minLength
  25. var term = extractLast( this.value );
  26. if ( term.length < 2 ) {
  27. return false;
  28. }
  29. },
  30. focus: function() {
  31. // prevent value inserted on focus
  32. return false;
  33. },
  34. select: function( event, ui ) {
  35. var terms = split( this.value );
  36. // remove the current input
  37. terms.pop();
  38. // add the selected item
  39. terms.push( ui.item.value );
  40. // add placeholder to get the comma-and-space at the end
  41. terms.push( "" );
  42. this.value = terms.join( ", " );
  43. return false;
  44. }
  45. });
  46. });
Add Comment
Please, Sign In to add comment