0

I'm trying to preventDefault a form made with Laravel Collective. The funny thing is that it works once and then it stops working. I'm working my js with Laravel Mix.

Here's my code:

// Here's the form in my blade
{!! Form::open(['class' => 'confirm-delete','method' => 'DELETE', 'route' => ['users.destroy', $user->id], 'data-title' => __('Delete User') ]) !!}
    <button type="submit" class="dropdown-item" href="#" style="cursor: pointer"><i class="fas fa-trash-alt"></i> {{ __('Delete') }}</button>
{!! Form::close() !!}

// Here's my code in my js

$(".confirm-delete").submit(function (e) {
    alert("It doesn't alert this D:");
    var form = this;
    e.preventDefault();

    swal({
        title: form.data("title"),
        icon: "warning",
        buttons: [
            'No, cancel it!',
            'Yes, I am sure!'
        ],
        dangerMode: true,
     }).then(function (isConfirm) {
        if (isConfirm) {
        form.submit();
        }
    });
});

I have tried with these links but none have worked:

https://stackoverflow.com/a/54062772/3675186

https://laracasts.com/discuss/channels/laravel/how-to-continue-to-submit-a-form-after-preventing-it-with-jquery

Using JQuery - preventing form from submitting

I'm working with Laravel 5.6.

Cœur
  • 37,241
  • 25
  • 195
  • 267
RalphVB
  • 103
  • 1
  • 10

1 Answers1

0

Finally got it. The problem was how I was getting the data attributes.

Here's the correct code:

$(".confirm-delete").on('submit', function (e) {
    var form = this;
    e.preventDefault();

    swal({
        title: $(this).attr("data-title"),
        text: "You will not be able to recover this imaginary file!",
        icon: "warning",
        buttons: [
            'No, cancel it!',
            'Yes, I am sure!'
        ],
        dangerMode: true,
    }).then(function (isConfirm) {
        if (isConfirm) {
            form.submit();
        }
    });
});

Hope it helps someone :)

RalphVB
  • 103
  • 1
  • 10