Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an ID from a button into a bootstrap modal

I've managed to figure out how to grab external json and use that in a modal using a manually set id in the javascript.

I'm stuck on passing the id from the button to the javascript url. My button markup is as follows;

<button type="button" class="btn btn-primary btn-sm modal-btn" data-id="10">Details</button>

My JS currently looks like this;

<script type="application/javascript">
       $(document).ready(function() {
var table = $('#example').DataTable( {

    dom: 'T<"clear">lfrtip',
    tableTools: {
        "sRowSelect": "multi",
        "aButtons": [ "select_all", "select_none" ],
        "aoColumnDefs": [
            { "bVisible": false, "aTargets": [ 1 ] }
        ]
    }
});


$('#example tbody').on( 'click', 'tr', function (e) {

    if($(e.target).is('.modal-btn')){
        $('#fullDetails').modal('show');
           var event = "details.php?id=" + ($(this).data('id'));
                $.getJSON(event, function( data ) {
                    $(".modal-title").text(data.title);
                    $(".modal-audience").text(data.audience);
                    $(".modal-leader").text(data.leader);
                    $(".modal-date").text(data.date);
                    $(".modal-start_time").text(data.start_time);
                    $(".modal-end_time").text(data.end_time);
                    $(".modal-details").text(data.details);
                    $(".modal-venue").text(data.venue);
                    $(".modal-cost").text(data.cost);
                }, "json" );

    }else{
        $(this).toggleClass('selected'); 
    }
});

$('#button').click( function () {
    alert( table.rows('.selected').data().length +' row(s) selected' );
});

}); </script>

the line im having issue with is;

var event = "details.php?id=" + ($(this).data('id'));

I'm using ($(this).data('id')); to try and grab the id attribute from the button, but i keep getting undefined.

like image 451
Nathan Avatar asked Apr 22 '15 13:04

Nathan


People also ask

How pass data from button to modal in Bootstrap?

When the submit button is clicked, it invokes the jQuery function. The data entered the text area is extracted using the val() method into the text variable. This text string is passed to the modal body using the html() method of jQuery.

How do you pass data onclick of a button to the modal?

modal', function (event) { var button = $(event. relatedTarget); // Button that triggered the modal var recipient = button. data('whatever'); // Extract info from data-* attributes // If necessary, you could initiate an AJAX request here (and then do the updating in a callback). // Update the modal's content.

How do you link a button to a modal?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.


1 Answers

Try

var event = "details.php?id=" + ($(e.target).data('id'));

$(this) is pointing to the tr that was clicked.

like image 127
Jacob Roberts Avatar answered Oct 26 '22 00:10

Jacob Roberts