Virajsinh

Select Dropdown Value Selected Using jQuery

Aug 27th, 2025 (edited)
2,193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.08 KB | Source Code | 0 0
  1. <script type="text/javascript">
  2.  
  3. let selectedId = $('#state_id').data('selected-id');
  4. if (selectedId > 0) {
  5.     $('#state_id').val(selectedId).trigger('change');
  6. }
  7.  
  8. window.initAllSelects = function() {
  9.     $('select[data-selected]').each(function() {
  10.         const $el = $(this);
  11.         let val = $el.data('selected');
  12.         if ($el.prop("multiple") && !Array.isArray(val)) {
  13.             val = [val];
  14.         }
  15.         $el.val(val).trigger("change");
  16.     });
  17. };
  18.  
  19. initAllSelects();
  20.  
  21. window.setSelectedValues = function(selectors)
  22. {
  23.     // Convert single string to array
  24.     if (typeof selectors === "string") {
  25.         selectors = [selectors];
  26.     }
  27.  
  28.     selectors.forEach(selector => {
  29.         const $element = $(selector);
  30.         if ($element.length) {
  31.             let selectedValue = $element.data("selected");
  32.  
  33.             // Skip if selectedValue is empty string or 0
  34.             if (selectedValue === "" || selectedValue === 0 || selectedValue === undefined) {
  35.                 return; // skip this element
  36.             }
  37.  
  38.             // Handle multi-select
  39.             if ($element.prop("multiple") && !Array.isArray(selectedValue)) {
  40.                 selectedValue = [selectedValue]; // convert single value to array
  41.             }
  42.  
  43.             $element.val(selectedValue).trigger("change");
  44.         }
  45.     });
  46. };
  47.  
  48.  
  49. // Single select
  50. setSelectedValues("#gender_id");
  51.  
  52. // Multiple select elements
  53. setSelectedValues([
  54.     "#gender_id",
  55.     "#blood_group_id",
  56.     "#marital_status_id",
  57.     "#nationality_id",
  58.     "#present_state_id",
  59.     "#permanent_state_id",
  60.     "#department_id",
  61.     "#designation_id",
  62.     "#job_type_id",
  63.     "#reporting_to",
  64.     "#shift_id",
  65.     "#status",
  66. ]);
  67.  
  68. // Container Inside Value Selected
  69.  
  70.     window.applySelectedValues = function(container)
  71.     {
  72.         // Convert single string to array
  73.         if (typeof container === 'string') {
  74.             container = [container];
  75.         }
  76.  
  77.         // If not array, make it an array
  78.         if (!Array.isArray(container)) return;
  79.  
  80.         container.forEach(c => {
  81.             const $container = c ? $(c) : $(document);
  82.  
  83.             $container.find('select[data-selected]').each(function ()
  84.             {
  85.                 let selected = $(this).data('selected');
  86.  
  87.                 if (selected === undefined || selected === null) return;
  88.  
  89.                 // If select is multiple and value is string → parse JSON
  90.                 if (this.multiple && typeof selected === 'string') {
  91.                     try {
  92.                         selected = JSON.parse(selected);
  93.                     } catch (e) {
  94.                         selected = [];
  95.                     }
  96.                 }
  97.  
  98.                 // Convert everything to string (important)
  99.                 if (Array.isArray(selected)) {
  100.                     selected = selected.map(String);
  101.                 } else {
  102.                     selected = String(selected);
  103.                 }
  104.  
  105.                 $(this).val(selected).trigger('change');
  106.             });
  107.         });
  108.     };
  109.  
  110. applySelectedValues('#earning_table'); // Single selector
  111. applySelectedValues(['#earning_table', '#deduction_table', '#contribution_table']); // Multiple selectors
  112.  
  113. <script>
Advertisement
Add Comment
Please, Sign In to add comment