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.