Advertisement
vapvarun

Preselecting and Locking an Option in a Selectize.js Multi-Select Dropdown with jQuery

Dec 17th, 2023
1,727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 0.94 KB | Software | 0 0
  1. <script>
  2.     jQuery(document).ready(function( $ ) {
  3.         // Function to set and lock the value
  4.         function setAndLockSelectize() {
  5.             var selectize = $('#bp-blog-category-select')[0].selectize;
  6.  
  7.             // Clear any previous selections
  8.             selectize.clear();
  9.  
  10.             // Add and select the value '81'
  11.             selectize.addOption({value: '81', text: 'Member Blog'});
  12.             selectize.addItem('81');
  13.  
  14.             // Lock the selectize control to prevent user interaction
  15.             selectize.lock();
  16.         }
  17.  
  18.         // Check if Selectize is initialized
  19.         if ($('#bp-blog-category-select').hasClass('selectized')) {
  20.             setAndLockSelectize();
  21.         } else {
  22.             // Wait for Selectize to initialize
  23.             $('#bp-blog-category-select').one('selectize:initialize', function() {
  24.                 setAndLockSelectize();
  25.             });
  26.         }
  27.     });
  28. </script>
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement