Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

simsar78's avatar

Select2 Not work if create modal dynamically with php

Hello, i create a modal with php dynamically at start page.

have this code for select2:

        $('#country).select2({
            theme: "bootstrap",
            dropdownParent: $("#ModalNew")
        });

If the modal is static it's work well.

0 likes
2 replies
Nakov's avatar

Dynamic means you can have multiple modals ? You can try to use a class instead of an id for the elements, if you happen to have more than one element on the same page using the same id.

By that I mean replace id="country" with class="country" and then use $('.country).select2({

or make those IDs dynamic as well, like country-ID and pass some ID of the item you are showing in the modal.

simsar78's avatar

I have 3 modal. But the combo box is for one modal. I try to class but the same.

Please or to participate in this conversation.