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

binggle's avatar

How to call alpine JS method from livewire?

hi

I want to run alpinejs function from livewire component.

in posts component,

function gotcomment(){
    $this->emit('gotcomment');
}

in posts view

<div wire:click="gotcomment">
    I will get post
</div>

in blade.

<div x-data="myfunction()">
    @livewire('posts')
</div>

<script>
    window.livewire.on('gotcomment', () => {
        console.log( '----')
        this.callme() ; --> it goes error ..
    });

    function myfunction() {
        return {
            callme() {
                console.log('callme')
            },
        }
    }
</script>

this.callme() is not run..

How can I fire alpinejs function from livewire ?

0 likes
7 replies
mrglazzz's avatar

I guess I don't understand it enough :( I thought I was using alpine :) I don't get how can I call alpine function from livewire how, after some changes on server, can I call callme() ?

frankielee's avatar
Level 29
       document.addEventListener('livewire:load', function () {

                Livewire.on('gotcomment', (el, component) => {
                    @this.callme();
                });
}
gezryl_'s avatar
<script>	
		const myObject= { 
				callme: function (){ 
						console.log('working);
				 },
		}
		document.addEventListener('livewire:load', function () { 
				Livewire.on('gotcomment', (el, component) => { 
						myObject.callme();
				});
		});
</script>

// livewire controller

public function gotcomment() {
		$this->emit('gotcomment'); 
}
1 like

Please or to participate in this conversation.