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

MrIncredible's avatar

Livewire plus scripts

Hello

I have trouble with scripts that initialize functionalities like select2 or datatables where i get a bug on my laravel project with livewire. I have setup the datatables script like this for example:

    <script>
        new DataTable('#usersTable', {
            responsive: true,
            language: {
                url: "{{ __('messages.tablesLang') }}",
                paginate: {
                    "first": "«",
                    "last": '\u203A',
                    "next": "»",
                    "previous": '\u2039'
                },
            }
        });
    </script>

or select2 like this

 <script>
        $(document).ready(function() {
            $('.create-user').select2({
                width: 'resolve'
            });
        });
    </script>

they work just fine, but i have like more than 1 route where i use the data tables and when i go to for example users which has datatables, and than i go to dashboard route and click back (to users route) the data tables have double everything initialized (paging, filters and all other options) or the select2 doubles its selects. I just want to know if this is a general livewire problem where navigating back to a route that has some external stuff initialized they get doubled. Cause i know what causes it, its wire:navigate that i put on links when navigating to other routes. If i remove it, it works fine but than the project doesnt have the SPA feeling anymore. Is there a workaround on how to use scripts with livewire.

Thank you for your understanding.

0 likes
4 replies
vincent15000's avatar

I already had such a problem with some divs in double when clicking on the back button of the browser.

It's not related to the scripts, but rather to the HTML code.

Not sure, I don't really remmeber ... but I think that I noticed that what got in double was some blade components.

I remember having solved this problem by removing these blade components and putting their code directly inside the page for which I had the problem.

MrIncredible's avatar

@vincent15000 Well for me it doesnt double blade components but just parts generated by datatables or select2 for example the datatable's options(pagination,search...) select2 get doubled after going back to a page which uses them.

1 like

Please or to participate in this conversation.