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

anonymouse703's avatar

Select2 not working on the child component

I have to component table and form when I run the test in the table(Parent) component the select2 is working but when I run the test in the form(Child) component form it will not run.

Test Select in the table and form

<div class="form-group">
    <label class="col-sm-3 control-label" for="customer_id">Country</label>
    <div class="col-sm-9">
        <select id="ex" class="form-control" data-placeholder="Choose a Country..." id="demo-chosen-select" tabindex="2">
            <option value="United States">United States</option>
            <option value="United Kingdom">United Kingdom</option>
            <option value="Afghanistan">Afghanistan</option>
            <option value="Aland Islands">Aland Islands</option>
            <option value="Albania">Albania</option>
            <option value="Algeria">Algeria</option>
            <option value="American Samoa">American Samoa</option>
            <option value="Andorra">Andorra</option>
            <option value="Angola">Angola</option>
            <option value="Yemen">Yemen</option>
            <option value="Zambia">Zambia</option>
            <option value="Zimbabwe">Zimbabwe</option>
        </select>
    </div>
</div>

I just include the javascript file the the table(Parent) component

@section('custom_script')
    @include('layouts.scripts.job-order-scripts');
@endsection

and this is my script


    $(function(){
        $('#ex').select2();
    });

Should I make another script to attached in the form(Child) component?

0 likes
27 replies
anonymouse703's avatar

@furqanDev Yes, I put in the webpack.mix

    'resources/admin/css/bootstrap.min.css',
    'resources/admin/css/nifty.min.css',
    'resources/admin/plugins/pace/pace.min.css',
    'resources/admin/plugins/font-awesome/css/font-awesome.min.css',
    'resources/admin/plugins/ionicons/css/ionicons.min.css',
    'resources/admin/plugins/themify-icons/themify-icons.min.css',
    'resources/admin/plugins/bootstrap-select/bootstrap-select.min.css',
    'resources/admin/plugins/chosen/chosen.min.css',
    'resources/admin/plugins/select2/css/select2.min.css',
    'resources/admin/DataTables/datatables.min.css',
],'public/css/main.css')
    .scripts([
        'resources/admin/js/jquery.min.js',
        'resources/admin/js/bootstrap.min.js',
        'resources/admin/js/nifty.min.js',
        'resources/admin/plugins/pace/pace.min.js',
        'resources/admin/plugins/flot-charts/jquery.flot.min.js',
        'resources/admin/plugins/flot-charts/jquery.flot.resize.min.js',
        'resources/admin/plugins/flot-charts/jquery.flot.tooltip.min.js',
        'resources/admin/plugins/sparkline/jquery.sparkline.min.js',
        'resources/admin/plugins/bootstrap-select/bootstrap-select.min.js',
        'resources/admin/plugins/chosen/chosen.jquery.min.js',
        'resources/admin/plugins/select2/js/select2.min.js',
        'resources/admin/DataTables/datatables.min.js',
], 'public/js/main.js');
Sinnbeck's avatar

Was that script mean to be self executing?

$(function(){
        $('#ex').select2();
    })(); //run at once
Sinnbeck's avatar

Any chance that they are shown at the same time? You cannot have 2 elements with the same ID. Use a class instead, or run the initializer once for each unique id

 $(function(){
        $('.selects').select2();
// or
        $('#ex1').select2();
        $('#ex2').select2();
    });
anonymouse703's avatar

@Sinnbeck What I mean the JS file is included in the table(parent) component and not in the form(child) component.

I have this sample in the table where I used id

    <label class="col-sm-3 control-label" for="customer_id">Country</label>
    <div class="col-sm-9">
        <select id="ex" class="form-control" data-placeholder="Choose a Country..." tabindex="2">
            <option value="United States">United States</option>
            <option value="United Kingdom">United Kingdom</option>
            <option value="Afghanistan">Afghanistan</option>
            <option value="Aland Islands">Aland Islands</option>
            <option value="Albania">Albania</option>
            <option value="Algeria">Algeria</option>
            <option value="American Samoa">American Samoa</option>
            <option value="Andorra">Andorra</option>
            <option value="Angola">Angola</option>
            <option value="Yemen">Yemen</option>
            <option value="Zambia">Zambia</option>
            <option value="Zimbabwe">Zimbabwe</option>
        </select>
    </div>

and in the form(child) component

<div class="form-group">
    <label class="col-sm-3 control-label" for="customer_id">Country</label>
    <div class="col-sm-9">
        <select class="demo-chosen-select form-control" data-placeholder="Choose a Country..." tabindex="2">
            <option value="United States">United States</option>
            <option value="United Kingdom">United Kingdom</option>
            <option value="Afghanistan">Afghanistan</option>
            <option value="Aland Islands">Aland Islands</option>
            <option value="Albania">Albania</option>
            <option value="Algeria">Algeria</option>
            <option value="American Samoa">American Samoa</option>
            <option value="Andorra">Andorra</option>
            <option value="Angola">Angola</option>
            <option value="Yemen">Yemen</option>
            <option value="Zambia">Zambia</option>
            <option value="Zimbabwe">Zimbabwe</option>
        </select>
    </div>
</div>

and the scripts for both

$(function(){
        $('#ex').select2();//table
        $('.demo-chosen-select').select2();//form
    });

Since it only work in the table(parent) component should I make an inline script in the form? or attached another script file?

Sinnbeck's avatar

@anonymouse703 That should be fine. But only the first one works or? Any chance that the second isnt shown at first render? If it is loaded later by livewire, then it wont work.

anonymouse703's avatar

@Sinnbeck This one is working in the parent $('#ex').select2();//table but when I click the the modal form (the child component) then this $('.demo-chosen-select').select2();//form wont working..

Sinnbeck's avatar

@anonymouse703 let me just repeat

Any chance that the second isnt shown at first render? If it is loaded later by livewire, then it wont work.

How is the modal shown/hidden? By class? If it isn't in the html source, it won't work (f12 -> Elements)

anonymouse703's avatar

@Sinnbeck Ah yes it was hidden in the first render sorry about that.

I did like this one

 <!-- The Modal -->
            <div wire.ignore.self class="modal fade" id="joborderModal" tabindex="-1" role="dialog"
                aria-labelledby="joborderModal" aria-hidden="true" data-backdrop="static" data-keyboard="false">
                <div class="modal-dialog modal-lg" role="document">
                    <livewire:j-o-m-s.job-order-form />
                </div>
            </div>
Sinnbeck's avatar

@anonymouse703 While the modal is open, try opening the console (F12->Console) and paste in $('.demo-chosen-select').select2() and hit enter. Does it work then?

anonymouse703's avatar

@Sinnbeck sorry sir I fall asleep yesterday... Yeah it's running..

I tried this approach


$('.demo-chosen-select').select2({
     dropdownParent: $('#joborderModal')
});

What JS hooks should I used? or I need to use livewire inline script?

Sinnbeck's avatar

@anonymouse703 sounds like it is a timing issue, if it works when you run it manually. You can try running it with a timeout

setTimeout(function () {
     //run the code 
}, 300)
anonymouse703's avatar
anonymouse703
OP
Best Answer
Level 6

@Sinnbeck Thanks sir I already fixed it.... I just put it in event and called when the modal open

document.addEventListener("DOMContentLoaded", () => {

        Livewire.on('select2', postId => {
            $('#customer').select2({
                dropdownParent: $("#joborderModal")
            });//
        })
        
}

Please or to participate in this conversation.