Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery.fn.filterByText = function(textbox) {
  2.   return this.each(function() {
  3.     var select = this;
  4.     var options = [];
  5.     $(select).find('option').each(function() {
  6.       options.push({
  7.         value: $(this).val(),
  8.         text: $(this).text()
  9.       });
  10.     });
  11.     $(select).data('options', options);
  12.  
  13.     $(textbox).bind('change keyup', function() {
  14.       var options = $(select).empty().data('options');
  15.       var search = $.trim($(this).val());
  16.       var regex = new RegExp(search, "gi");
  17.  
  18.       $.each(options, function(i) {
  19.         var option = options[i];
  20.         if (option.text.match(regex) !== null) {
  21.           $(select).append(
  22.             $('<option>').text(option.text).val(option.value)
  23.           );
  24.         }
  25.       });
  26.     });
  27.   });
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement