Jquery-chosen is not working on ajax appended select

Viewed 1553

I am facing a strange problem regarding Jquery-Chosen. I have a multi select box inside a pop-up whose options are populated using ajax call. Unfortunately Jquery-Chosen is not working on it. But if I use a static multi select box in the same place, it works fine. I have googled a lot but can not find a solution.

Following is my HTML code -

<select multiple class="tid form-control assign_teacher_dropdown" name="assign_teacher[]">
</select>

Following is my Jquery code -

$.ajax({
    url: '/getclass/'+id,
    type: 'get',
    dataType: 'json',
    success: function (response) {
        if(response.status == '200') { 
            var class_info = response.class;                   
            var teacher_info = class_info.teacher;
            $('.assign_teacher_dropdown').empty();
            $('.ttterm_dropdown').empty();
            if(!teacher_info){
                $('.assign_teacher_dropdown').append('<option value="">Please select one</option>');
            }
            $('.class-id').val(class_info.id);
            $('.class-name').val(class_info.class_name);
            $('.class-code').val(class_info.class_code);
            $('.start-date').val(class_info.start_date);
            $('.end-date').val(class_info.end_date);
            var teacherDropdown = '';
            $.each(response.teachers, function( index, value ) {
                var optionSelected = '';
                if(teacher_info) {
                    if(response.selected_teacher.indexOf(index) !=-1){
                        var optionSelected = 'selected';                            
                    }
                }
                teacherDropdown = '<option '+optionSelected+' value="'+index+'">'+value+'</option>';
                $('.assign_teacher_dropdown').append(teacherDropdown);

            });

            $.each(response.terms, function( index, term ) {
                var optSelected = '';
                if(term) {
                    if(index == class_info.term_id){
                        var optSelected = 'selected';
                    }
                }
                var termDropdown = '<option '+optSelected+' value="'+index+'">'+term+'</option>';
                $('.tterm_dropdown').append(termDropdown);
            });

            $('.chosen-container-single-nosearch').hide();
            $('.assign_teacher_dropdown').show();
            $('.tterm_dropdown').show();
            $('#class-update-modal-update').modal('show');
        }
        else {
            validation.html(getMessage(response.message, 'danger'));
        }
        window.scrollTo(0, 0);
        $(".chosen-select").chosen(); ////for static multi select which is working fine
        $('.assign_teacher_dropdown').chosen(); ////it is not working
    }
});

The response -

response.teachers = Object { 2ee71930-70f9-11e7-8040-0744cf2f827d: "Raihan2 Razi2", 47e2f900-6c7d-11e7-98d2-ebfd03a47749: "Teacher -1", 5873df40-7028-11e7-a3a6-4b6f2ca6ed6b: "Teacher Akter", 0ad51850-7107-11e7-a380-8b0b33c8ce62: "Rakib-3 Hasan-3", 63cd0d30-7105-11e7-b09d-a7232d9caeba: "Rakib-2 Hasan-2", 6c2ab790-70f7-11e7-a4f5-aba1de53d767: "Raihan1 Razi1", a6a47520-70f3-11e7-85e1-d760ee2c8a38: "Raihan Razi", 331ba070-7114-11e7-bdd1-2107af6559b4: "Rakib-4 Hasan-4", cbf1d490-7028-11e7-bab1-97745facf387: "Teacher Mizan" }
teacher_info = {id: "5873df40-7028-11e7-a3a6-4b6f2ca6ed6b", email: "akter@wearedando.com", first_name: "Teacher", last_name: "Akter", dob: null, …}

The output -

The output

N.B.: The multi select is located inside a modal.

N.B.: Static multi select works fine in the same position.

Any help is highly appreciated. Thanks in advance.

So far what I have tried -

  1. Taken all the options in a variable and used .html() rather than appending
  2. Tried assigning a dynamic class along with .assign_teacher_dropdown and called .chosen() on that dynamic class.
  3. Separated the .chosen() from the ajax call and called it using a separate function.
1 Answers

The reason it doesn't work is because the function chosen() is called on the dynamically created popup before any options are added. When you use the $.each function it returns immediately and calls the predicate function asynchronously. You need to wait until all options have been added before calling chosen()

Or you can use their dynamic API before adding options $("#form_field").trigger("chosen:updated");

Related