Advertisement
Virajsinh

Bootstrap Select Dropdown Dynamic Options Add Using jQuery

Aug 6th, 2024
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.99 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  5. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.18/css/bootstrap-select.min.css">
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  8. <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.18/js/bootstrap-select.min.js"></script>
  9. </head>
  10. <body>
  11.  
  12. <select class="selectpicker" id="dynamicSelect" data-live-search="true">
  13.     <!-- Initial options if any -->
  14.     <option value="1">Option 1</option>
  15.     <option value="2">Option 2</option>
  16. </select>
  17.  
  18. <script>
  19.  
  20. $(document).ready(function() {
  21.     // Initialize the selectpicker with a custom no results message
  22.     $('#dynamicSelect').selectpicker({
  23.         noneResultsText: 'No results matched "{0}"'
  24.     });
  25.  
  26.     // Function to add a new option to the selectpicker
  27.     function addOptionToSelectpicker(value, text) {
  28.         var newOption = $('<option>', {
  29.             value: value,
  30.             text: text
  31.         });
  32.  
  33.         $('#dynamicSelect').append(newOption).selectpicker('refresh');
  34.     }
  35.  
  36.     // Function to handle the input and enter key logic
  37.     function handleSelectpickerInput(event) {
  38.         var searchValue = $(event.target).val().toLowerCase();
  39.         var optionExists = false;
  40.  
  41.         if (event.key === "Enter" && searchValue.trim() !== '') {
  42.            // Check if the option already exists
  43.            $('#dynamicSelect option').each(function() {
  44.                if ($(this).text().toLowerCase() === searchValue) {
  45.                    optionExists = true;
  46.                     $('#dynamicSelect').selectpicker('val', $(this).val());
  47.                     $('#dynamicSelect').selectpicker('refresh');
  48.                     return false; // Break the loop
  49.                 }
  50.             });
  51.  
  52.             // If the option does not exist, add it
  53.             if (!optionExists) {
  54.                 var newOptionValue = searchValue;
  55.                 var newOptionText = searchValue.charAt(0).toUpperCase() + searchValue.slice(1);
  56.  
  57.                 addOptionToSelectpicker(newOptionValue, newOptionText);
  58.                 $('#dynamicSelect').selectpicker('val', newOptionValue);
  59.                 $('#dynamicSelect').selectpicker('refresh');
  60.             }
  61.  
  62.             // Clear the input field
  63.             $(event.target).val('');
  64.         }
  65.     }
  66.  
  67.     // Event listener for the input field of the selectpicker
  68.     $('.bs-searchbox input').on('keyup', function(event) {
  69.         var searchValue = $(this).val();
  70.         var noneResultsText = 'Press Enter to add "' + searchValue + '"';
  71.  
  72.         $('#dynamicSelect').data('selectpicker').options.noneResultsText = noneResultsText;
  73.         $('#dynamicSelect').selectpicker('render');
  74.  
  75.         handleSelectpickerInput(event);
  76.     });
  77. });
  78. </script>
  79.  
  80. </body>
  81. </html>
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement