bluzky / nice-select2

A lightweight vanilla javascript library that replaces native select elements with customizable dropdowns
https://bluzky.github.io/nice-select2/
MIT License
370 stars 61 forks source link

multiple initiating in Modal #80

Open Shuraih-Usman opened 7 months ago

Shuraih-Usman commented 7 months ago

I am using it in multiples modal items, with the same class, but each modal has it's datas, so if i open one modal, and open another onea again, then it would appeared twice, if i open another modal again it would again appeared, i used empty(); function to empty it but it does not work. i am using it direct from ajax

<select name="category" id="edit-wish-cats" class="wide"> </select>

this is the html

` function initSelect2(id, type, pid ='') {

var selectID = $("#"+id);
let ids = '';
var isID = pid.length > 0;
if(isID) {
  ids = pid;
}

if(selectID) {
    $('#'+id).empty();
    $.ajax({
        type: 'POST',
        url: ajaxURL,
        headers: {
            'X-CSRF-TOKEN': csrfToken
        },
        data: {
          action : 'getselect',
          type: type,
          id: ids,
        },
        success: function(html){
          $('#'+id).empty();
            $('#'+id).append(html);
             NiceSelect.bind(document.getElementById(id), {searchable:true});

        },
        error: function (xhr, status, error) {
          console.log(xhr.responseText || error);
      }
    }); 
}

}`

this is the JS in function i called function here

initSelect2('edit-wish-cats', 'wishlists_types', data.wish_cat);

Screenshot 04-03-2024 04 18 31