skip to Main Content

Alert before button click

<button type="button" wire:click="remove({{ $user->id }})" onclick="return confirm('Are you sure you want to remove this?');" >
    Remove User
</button>

2

Answers


  1. Chosen as BEST ANSWER

    and move the wire:click after. It won’t get hit if the confirmation is cancelled.

    onclick="confirm('Are you sure you want to remove this?') || event.stopImmediatePropagation()"


  2. You can use Livewire’s wire:click event in combination with JavaScript for confirmation

    <button type="button" onclick="remove(event, {{ $user->id }})">
        Remove User
    </button>
    
    <script>
        function remove(event, userId) {
            if (confirm('Are you sure you want to remove this?')) {
                // confimred
                console.log('confirmed');
                
            } else {
                // Prevent the default action if not confirmed
                event.preventDefault();
            }
        }
    
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search