Nathan
Nathan

Reputation: 2513

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.

Upvotes: 5

Views: 1829

Answers (3)

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29683

Try to fetch it as below

$('#example tbody').on( 'click', 'tr', function (e) {
      var target = $( e.target );//Try this change

    if(target.is('.modal-btn')){ //compare it this way
        $('#fullDetails').modal('show');
           var event = "details.php?id=" + (target.data('id'));//fetch it like this
                $.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'); 
    }
});

Upvotes: 2

nada
nada

Reputation: 972

Change the line to

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

and try again

Upvotes: 0

Jacob Roberts
Jacob Roberts

Reputation: 1815

Try

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

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

Upvotes: 4

Related Questions