1

I am using select2 jquery function. To create a drop down with multiple selections. I am able to do it. Now I need to select a few options from the drop down list as default data. Can you help me?

data = [{id:1,text:"abc"},{id:2,text:"qwe"},{id:3,text:"def"}]; 
 
 $("#test").select2({ theme: "bootstrap", data: dat });
 
 
 
 // select option 2 as default selected I used below code
 // but it is not working..  
   $("#test").select2("val", 2);
nimra asad
  • 179
  • 6
  • 18
  • 1
    This might help : http://stackoverflow.com/questions/12889552/how-to-set-selected-value-in-multi-value-select-in-jquery-select2 – Sagar Hani May 13 '17 at 12:38
  • 1
    As mentioned in this http://stackoverflow.com/a/30477163/1225070 the .select2('val') The "val" method has been deprecated and will be removed in Select2 4.1. You can try $('#test').val('1').trigger('change'); – Aefits May 13 '17 at 12:47
  • yes this does help... but I am not able to select multiple values in 'val' :( – nimra asad May 13 '17 at 12:52
  • 1
    did you add multiple: true ? In order to select multiple values you must add this . – Aefits May 13 '17 at 13:24
  • 1
    @nimraasad. Glad to know you problem is solved. – Aefits May 15 '17 at 07:05

0 Answers0