How to send the id to a modal

   <table id="example" class="display" style="width:100%">
                        @foreach ($productos as $producto)


                                    <a  class="btn btn-primary btn-group-sm col-lg-offset-2 active"
                                       data-toggle="modal" href="#modal-eliminar{{$producto->id}}"
                                       > <i class="fa fa-trash"></i>


This is my modal where I receive the parameter the problem that I have is that if I detect the id that I select but then I will not have the modal if I remove {{$ product-> g}} from the href and the modal that receives it there if it opens

<div class="modal fade" id="modal-eliminar{{$producto->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header text-center">
                <h4 class="modal-title w-100 font-weight-bold">Productos Eliminar</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
            <div class="modal-body mx-6">
asked by user9472850 13.12.2018 в 04:28

2 answers


I think that in order to pass data to the modal one you should use data-target="# modal-delete" inside the tag instead of using href and pass the data through data-data, in the following way:

<a href="#" data-toggle="modal" data-target="#modal-eliminar" data-datos="datosapasar" data-datos2="pasarotrosdatos"> Mi link</a>

We can then process this past data with jquery and assign them to any modal field:

$('#modal-eliminar').on('', function (event) {
        var etiqueta = $(event.relatedTarget)
        var recipient ='datos')
        var recipient2 ='datos2')

        var modal = $(this)
        //Guardamos el resultado en un input dentro del modal de nombre mnombre
        modal.find('.modal-body #mnombre').val(recipient)


answered by 13.12.2018 в 09:31

There is only one modal, so its id must be id="modal-eliminar" , regardless of the item that is to be deleted.

What you would miss is to indicate (by javascript) at the time of pressing, what is the id that has been pressed.

It could be something like:

In the button, we add a class product_del , to add the event click and a custom attribute data-product_id with the value of id of the product associated with the button.

<a  class="btn btn-primary btn-group-sm col-lg-offset-2 active product_del"
    data-toggle="modal" href="#modal-eliminar" data-product_id="{{$producto->id}}"
    > <i class="fa fa-trash"></i>

in javascript we add the 'click' event:

$('.product_del').click(function (event) {
    var id = $('product_id');
    // faltaría asignar el id a algún elemento del modal
    // actualmente el modal esta vacío y no hace nada 
answered by 13.12.2018 в 09:46