Search code examples
phplaravelsweetalert2

How do I make the whole confirm button a link in sweetalert2 in laravel controller


I have a sweetalert from https://realrashid.github.io/sweet-alert/ that I am using in my controller. My confirm button only works when you click the actual 'Yes' text as this is where the link is. If I click anywhere else on the confirm button the alert just closes. I would like the whole button to go through to the link. Thank you.

public function show($id) {
  $user = User::find($id);

  Alert::warning('Deleting user -<br/>are you sure?')
    ->showCancelButton($btnText = 'Cancel', $btnColor = '#dc3545')
    ->showConfirmButton($btnText = '<a href="/admin/users/'. $id .'/delete">Yes</a>', $btnColor = '#38c177')
    ->autoClose(false);

  return redirect()->route('users.index');
}

Solution

  • Seems that library has no API to add any actions, so, you can use dirty hack:

    Add some class for your link, add class to button too:

    Alert::warning('Deleting user -<br/>are you sure?')
        ->showCancelButton($btnText = 'Cancel', $btnColor = '#dc3545')
        ->showConfirmButton(
            $btnText = '<a class="add-padding" href="/admin/users/'. $id .'/delete">Yes</a>', // here is class for link
            $btnColor = '#38c177',
            ['className'  => 'no-padding'], // add class to button
        )->autoClose(false);
    

    Set styles for that class in CSS:

    .no-padding {padding:0 !important;}
    
    .add-padding {
        padding: .625em 2em; # padding as you need according your styles
        display: inline-block;
    }