Select2 dropdownParent based on select position

Is it possible to do something like this? $(‘div’) works but not $(this).closest(‘div’)

$(‘select’).select2({
dropdownParent: $(this).closest(‘div’)
});

It’s not clear to me that this refers to the select element within the ‘select2()` method call, which is probably why this approach isnt’ working. Have you tried:

$('select').select2({
    dropdownParent: $('select').closest('div')
});

Alternatively, you could grab the ancestor div first and use that in the initializer:

let $ancestorDiv = $('select').closest('div');
$('select').select2({
    dropdownParent: $ancestorDiv
});