0

I try to use select2 for dynamic data received via ajax, in response I get the error that I wrote above. What am I doing wrong ? here is code :

$(document).on('change', '[name="country"]', function () {
    var vehicle_id = $(this).val();
    $.ajax({
        type: 'GET',
        url: '{{ route('select_vehicle') }}',
        data: { vehicle_id: vehicle_id },
        success: function (data) {
            $('.dyn').html(data);
        }
    });
    //  $('.dyn').on('DOMNodeInserted', 'select', function () {$(this).select2();});
    var target = $('.dyn'); //elm = $('.select2');
    const config = { childList: true };
    if (target) {
        let observer = new MutationObserver(function (mutations) {
            mutations.forEach(function (mutation) {
                if (mutation && mutation.addedNodes) {
                    mutation.addedNodes.forEach(function (elm) {
                        if (elm && elm.nodeName === "select") { $(elm).select2(); }
                    });
                }
            });
        });
        observer.observe(target, config);
    }
});

Thanks for your attention.

cbalakus
  • 620
  • 7
  • 15
Max
  • 35
  • 5
  • ['observe' on 'MutationObserver': parameter 1 is not of type 'Node'](https://stackoverflow.com/a/51917542) – wOxxOm Feb 02 '21 at 10:00

1 Answers1

0

I solved the problem.I put in dynamic results new div with id #inits.It works!Thanks for your attention.

 $('#inits select.select2').select2();}
Max
  • 35
  • 5