Recover captured id with JS in modal

Asked

Viewed 200 times

0

I have a button calling a modal for exclusion confirmation, this is in a table, each row a button to delete the registration of that line

But the way it is he always deletes the first item from the list

<button type="button" id="btn-modal" cidadao-id="{{$cidadao->id}}" class="btn-modal" data-toggle="modal" data-target="#modal-default">{{$cidadao->id}}</button>

<div class="modal fade" id="modal-default">
    <div class="modal-dialog">
        <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">Confirmação</h4>
        </div>
        <div class="modal-body">
            <p>Deseja realmente excluir o cadastro?</p>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Fechar</button>
            <form id="formdelete" class="form-vertical" method="POST" action="{{ action('CidadaoController@destroy', $cidadao->id)}}">
                @csrf
                @method('DELETE')                                               
                <button type="submit" class="btn btn-primary">{{$cidadao->id}}</button>
            </form>
        </div>
        </div>
    </div>
</div>

I managed to capture the correct id with JS:

$('.btn-modal').click(function(){
    var id = $(this).attr('cidadao-id');
    console.log(id);
    //document.formdelete.action = "{{ action('CidadaoController@destroy', id)}}";
})

I tried to replace the action to use the id I captured, but it didn’t work

How could I delete the registration of the captured id?

  • I have tested how it is and has worked https://jsfiddle.net/ph3rx4bc/1/

  • 1

    I only recommend changing citizenship-id to date-citizenship-id and use instead of attr data https://jsfiddle.net/ph3rx4bc/4/

  • @Wictorchaves thanks I’ll do it, but as for recovering the id in modal I wouldn’t know how?

1 answer

1


I got

This is how the form:

<form class="formdelete" name="formdelete" method="POST">
    @csrf
    @method('DELETE')                                               
    <button type="submit" class="btn btn-primary">Excluir</button>
</form>

And so in the JS:

$('.btn-modal').click(function(){
    var id = $(this).data('cidadao-id');
    //console.log(id); 
    $(".formdelete").attr('action', ("cidadaos/"+id));
})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.