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

vincent15000's avatar

Drag and drop and Livewire 3

Hello,

I have a Livewire component that displays a list (with Livewire 3).

I have developed the drag and drop manually and the script is loaded when the Livewire component loads.

I can add a new item in the list without refreshing manually the page.

It's impossible to drop an item on the newly added item.

How can I solve this problem ?

I already had the same problem one year ago, but not solved yet and I had used the livewire-sortable package.

https://laracasts.com/discuss/channels/livewire/livewire-sortable-with-dynamic-list

Thanks for your help.

V

0 likes
2 replies
MohamedTammam's avatar

Mostly, that's because the JavaScript code didn't detect the new item.

There're two possible solutions:

1- Instead of listening to an even on the element itself. You should listen to document events and check it's the element you want.

Example:

// Normal way
document.querySelector('.btn').addEventListener('click', () => // do something ..

// Solution for newly added elements
document.addEventListener('click', (e) => {
	if( e.target.matches('.btn, .btn *')) {
		// Do you logic
	}
});

2- Firing an event from livewire to re-run the JavaScript code that's responsible of the drag-drop logic.

1 like
vincent15000's avatar

@MohamedTammam I have fired an event from Livewire to re-run the JS script, but it doesn't work.

Perhaps it's just because I load the script before the DOM is entirely reloaded with the last adde item.

I have searched around the Livewire JS API and tried to listen to the livewire:init or the livewire:initialize events, in both case the script is reloading, but the newly added item doesn't detect the dragover or the drop event.

Please or to participate in this conversation.