Skip to content Skip to sidebar Skip to footer

Set Multiple Select2 Options

I have a form to create new Objects, which gives the possibility to add multiple Keywords via a Select2. I use Symfony ; the Keywords are taken from an entity. Adding existing keyw

Solution 1:

Well...

I continued to search and by chance I found something which seems to work (i.e. I didn't find a case where it doesn't, yet).

I changed the order of the lines and added a "clear Select2" instruction:

functionOnSuccess(result){
    $('#addKeyword').modal('hide');  
    $('#webobs_eventbundle_event_keywords').append('<option value="'+result.id+'" selected="selected">'+result.name+'</option>'); // Add the new Keyword in the listvar keywordsSelected = $("#webobs_eventbundle_event_keywords").select2("val");
    keywordsSelected.push(result.id);
    $("#webobs_eventbundle_event_keywords").select2("val", null);
    $("#webobs_eventbundle_event_keywords").select2("val", keywordsSelected);     
}

Anyway, I'm still interested in you opinion about this problem, because I don't really see the reason why this code works and not the first one...

Post a Comment for "Set Multiple Select2 Options"