Retrieve captured id with JS in modal

0

I have a button that calls a modal to confirm deletion, this is in a table, each line a button to exclude the registration of that line

But the way it is, it 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 was able 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 that I captured, but it did not work

How could I delete the captured id's record?

    
asked by anonymous 13.04.2018 / 21:33

1 answer

1

I was able

The form was thus:

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

And so on JS:

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