Advertisement
Virajsinh

Select2 Ajax Value Preselected/Selected Value Using jQuery/JavaScript

Mar 29th, 2024 (edited)
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.44 KB | Source Code | 0 0
  1. // https://select2.org/programmatic-control/add-select-clear-items
  2.  
  3. // Select2 Ajax Value Preselected/Selected Value Using jQuery/JavaScript
  4.  
  5. var data = {
  6.     id: 1,
  7.     text: 'Barn owl'
  8. };
  9.  
  10. var newOption = new Option(data.text, data.id, false, false);
  11. $('#mySelect2').append(newOption).trigger('change');
  12.  
  13. Multiple Select First Options
  14. $('#addForm select').each(function() {
  15.     $(this).prop('selectedIndex', 0).trigger('change');
  16. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement