Add select2 to dynamic content

i ran into a problem with adding select2 to dynamicaly added content.

My HTML:

    <table class="table d-none" data-kt-element="item-template">
<tr class="border-bottom border-bottom-dashed" data-kt-element="item">
    <td class="pe-7">
        <select class="form-select form-select-solid" data-control="select2" data-placeholder="Pasirinkite prekę" name="product_select[]" data-kt-element="product_select">
            <option></option>
            <option value="123">Opcij231313132a 1</option>
            <option value="321">Opcija 2</option>
        </select>
    </td>
    <td class="ps-0">
        <select class="form-select form-select-solid" data-control="select2" data-hide-search="true" data-placeholder="Vnt" name="matvnt[]" data-kt-element="matvnt">
            <option value=""></option>
            <option value="1">kg.</option>
            <option value="2">vnt</option>
        </select>
    </td>
    <td class="ps-0">
        <input class="form-control form-control-solid" type="number" placeholder="Pasirinkite datą" name="due_date[]" data-kt-element="due_date" id="due_date" />
    </td>
    <td class="ps-0">
        <select class="form-select form-select-solid" data-control="select2" data-hide-search="true" data-placeholder="Vnt" name="galiojimas[]" data-kt-element="galiojimas">
            <option value=""></option>
            <option value="1">12h</option>
            <option value="2">24h</option>
        </select>
    </td>
    <td class="ps-0">
        <input class="form-control form-control-solid" type="number" min="1" name="kiekis[]" placeholder="1" data-kt-element="kiekis" />
    </td>
    <td class="ps-0">
        <select class="form-select form-select-solid" data-control="select2" data-hide-search="true" data-placeholder="Vnt" name="temp[]" data-kt-element="temp">
            <option value=""></option>
            <option value="1">kg.</option>
            <option value="2">vnt</option>
        </select>
    </td>
    <td class="ps-0">
        <select class="form-select form-select-solid" data-control="select2" data-hide-search="true" data-placeholder="Vnt" name="rezim[]" data-kt-element="rezim">
            <option value=""></option>
            <option value="1">kg.</option>
            <option value="2">vnt</option>
        </select>
    </td>
    <td class="pt-5 text-end">
        <button type="button" class="btn btn-sm btn-icon btn-active-color-primary" data-kt-element="remove-item">
            <!--begin::Svg Icon | path: icons/duotone/General/Trash.svg-->
            <span class="svg-icon svg-icon-3">
                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                    <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
                        <rect x="0" y="0" width="24" height="24" />
                        <path d="M6,8 L6,20.5 C6,21.3284271 6.67157288,22 7.5,22 L16.5,22 C17.3284271,22 18,21.3284271 18,20.5 L18,8 L6,8 Z" fill="#000000" fill-rule="nonzero" />
                        <path d="M14,4.5 L14,4 C14,3.44771525 13.5522847,3 13,3 L11,3 C10.4477153,3 10,3.44771525 10,4 L10,4.5 L5.5,4.5 C5.22385763,4.5 5,4.72385763 5,5 L5,5.5 C5,5.77614237 5.22385763,6 5.5,6 L18.5,6 C18.7761424,6 19,5.77614237 19,5.5 L19,5 C19,4.72385763 18.7761424,4.5 18.5,4.5 L14,4.5 Z" fill="#000000" opacity="0.3" />
                    </g>
                </svg>
            </span>
            <!--end::Svg Icon-->
        </button>
    </td>
</tr>

My JS:

var handeForm = function(element) {
// Add item
form.querySelector('[data-kt-element="items"] [data-kt-element="add-item"]').addEventListener('click', function(e) {
    e.preventDefault();

    var item = form.querySelector('[data-kt-element="item-template"] tr').cloneNode(true);

    form.querySelector('[data-kt-element="items"] tbody').appendChild(item);

    handleEmptyState();
    updateTotal();
});

// Remove item
KTUtil.on(form, '[data-kt-element="items"] [data-kt-element="remove-item"]', 'click', function(e) {
    e.preventDefault();

    KTUtil.remove(this.closest('[data-kt-element="item"]'));

    handleEmptyState();
    updateTotal();
});

// Handle price and quantity changes
KTUtil.on(form, '[data-kt-element="items"] [data-kt-element="quantity"], [data-kt-element="items"] [data-kt-element="price"]', 'change', function(e) {
    e.preventDefault();

    updateTotal();
});

}

var initForm = function(element) { // Due date. For more info, please visit the official plugin site: https://flatpickr.js.org/ var invoiceDate = $(form.querySelector('[name="invoice_date"]')); invoiceDate.flatpickr({ enableTime: false, dateFormat: "d, M Y", });

Any ideas how would it be possible to fix this? Thanks in advance!



Read more here: https://stackoverflow.com/questions/68462717/add-select2-to-dynamic-content

Content Attribution

This content was originally published by nexus_one at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: