Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. <select name="airfromcity" class="form-control ui-autocomplete-input" id="airfromcity" autocomplete="off" style="display: none;">
  2. <option value="">Select one...</option>
  3. <option value="ABE">Allentown PA</option>
  4. <option value="ABI">Abilene TX</option>
  5. <option value="ABQ">Albuquerque NM</option>
  6. <option value="ABR">Aberdeen SD</option>
  7. <option value="ACV">Eureka CA</option>
  8. <option value="ACY">Atlantic City NJ</option>
  9. <option value="AEX">Alexandria VA</option>
  10. <option value="AGR">Agra India</option>
  11. <option value="AGS">Augusta GA</option>
  12. <option value="AHN">Athens GA</option>
  13. <option value="AIY">Atlantic City NJ -Bader</option>
  14. </select>
  15.  
  16. (function( $ ) {
  17. $.widget( "custom.combobox", {
  18. _create: function() {
  19. this.wrapper = $( "<span>" )
  20. .addClass( "custom-combobox" )
  21. .insertAfter( this.element );
  22.  
  23. this.element.hide();
  24. this._createAutocomplete();
  25. this._createShowAllButton();
  26. },
  27.  
  28. _createAutocomplete: function() {
  29. var selected = this.element.children( ":selected" ),
  30. value = selected.val() ? selected.text() : "";
  31.  
  32. this.input = $( "<input>" )
  33. .appendTo( this.wrapper )
  34. .val( value )
  35. .attr( "title", "" )
  36. .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
  37. .autocomplete({
  38. delay: 0,
  39. minLength: 0,
  40. source: $.proxy( this, "_source" )
  41. })
  42. .tooltip({
  43. tooltipClass: "ui-state-highlight"
  44. });
  45.  
  46. this._on( this.input, {
  47. autocompleteselect: function( event, ui ) {
  48. ui.item.option.selected = true;
  49. this._trigger( "select", event, {
  50. item: ui.item.option
  51. });
  52. },
  53.  
  54. autocompletechange: "_removeIfInvalid"
  55. });
  56. },
  57.  
  58. _source: function( request, response ) {
  59. var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
  60. response( this.element.children( "option" ).map(function() {
  61. var text = $( this ).text();
  62. if ( this.value && ( !request.term || matcher.test(text) ) )
  63. return {
  64. label: text,
  65. value: text,
  66. option: this
  67. };
  68. }) );
  69. },
  70.  
  71. _removeIfInvalid: function( event, ui ) {
  72.  
  73. // Selected an item, nothing to do
  74. if ( ui.item ) {
  75. return;
  76. }
  77.  
  78. // Search for a match (case-insensitive)
  79. var value = this.input.val(),
  80. valueLowerCase = value.toLowerCase(),
  81. valid = false;
  82. this.element.children( "option" ).each(function() {
  83. if ( $( this ).text().toLowerCase() === valueLowerCase ) {
  84. this.selected = valid = true;
  85. return false;
  86. }
  87. });
  88.  
  89. // Found a match, nothing to do
  90. if ( valid ) {
  91. return;
  92. }
  93.  
  94. // Remove invalid value
  95. this.input
  96. .val( "" )
  97. .attr( "title", value + " didn't match any item" )
  98. .tooltip( "open" );
  99. this.element.val( "" );
  100. this._delay(function() {
  101. this.input.tooltip( "close" ).attr( "title", "" );
  102. }, 2500 );
  103. this.input.data( "ui-autocomplete" ).term = "";
  104. },
  105.  
  106. _destroy: function() {
  107. this.wrapper.remove();
  108. this.element.show();
  109. }
  110. });
  111. })( jQuery );
  112.  
  113. $(function() {
  114. $( "#airfromcity" ).combobox();
  115. $( "#toggle" ).click(function() {
  116. $( "#airfromcity" ).toggle();
  117. });
  118. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement