I can populate the select2 but after I receive the response from ajax I need to click out of select2 and click inside select2 element to se the new options available, they don’t populate while i’m typing my search
HTML:
<fieldset class="form-group position-relative has-icon-left maxlengthone" id="fieldset_profile_select">
<select class="max-length-one form-control form-group position-relative has-icon-left pl-4" multiple="multiple" id="profile_select">
</select>
<div class="form-control-position">
<i class="fa fa-search"></i>
</div>
</fieldset>
Javascript:
$(document).ready(function () {
let fieldset = document.getElementById('fieldset_profile_select')
let inputSearch = fieldset.getElementsByTagName('input')[0]
inputSearch.addEventListener('change', function(){
refreshProfileSelect(inputSearch.value)
})
function refreshProfileSelect(search) {
document.getElementById('profile_select').append('')
console.log('teste')
$.ajax({
dataType: 'json',
delay: 250,
type: "GET",
data: {
'search': search,
'sid': 2
},
url: '/getProfiles',
success: function (response) {
response.map(function(obj) {
let option = document.createElement('option')
option.value = obj.id
option.innerHTML = `${obj.first_name} ${obj.last_name}`
document.getElementById('profile_select').append(option)
})
}
});
}
})