Take data from the button in jQuery and call Modal

Asked

Viewed 1,158 times

1

I have the following button:

<button type="button" class="btn btn-danger btn-custom" name="deletar" id="<? echo $valor->set_cod; ?>"><i class="fa fa-trash"></i></button>

I need to get the id="", and after fetching the id, I would like to call the bootstrap modal itself, with the delete message, which in turn, does the action when the OK button is pressed. How do I do?

2 answers

1


You can use the attributes data-href with the url to delete the record. Ex: data-href="delete.php? id=set_cod; ?>"

With jquery the following code, ensures the action when confirming

$('#nome-sua-modal').on('show.bs.modal', function(e) {
  //btn-ok é a classe do botão confirmar na modal
  $(this).find('.btn-ok').attr('href',        $(e.relatedTarget).data('href'));
});

Here is a complete example: example

  • Perfect answer, that’s right. Thank you!

1

To open a modal in the bootstrap you use the data-target:

<button type="button" class="btn btn-primary btn-lg" 
    data-toggle="modal" data-target="#myModal">

 <div class="modal fade" id="myModal" 
    tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
 </div>

If you need to accomplish something in the event click the button you can use the onclick=minhaFuncao(id) passing id to function.

function minhaFuncao(id) {
    // fazer algo aqui
}

If the modal id is obtained via php, you can use the same way to pass the id to both cases (button data-target and modal id).

Browser other questions tagged

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