Sol Sol - 3 months ago 6
jQuery Question

How can you pull data through on bootstrap buttons to modals?

I was having a hard time figuring out how to make this work. This should work from some examples I have seen. It does do an alert when the modal pops up, so what am I missing?

http://www.bootply.com/Hveakq42Vd

<a href="#" data-mylink="1" data-mylink2="2" data-mylink3="3" class="btn btn-success" role="button" data-toggle="modal" data-target="#modalmain">Print</a>

<!-- modal pop up for [F] button -->
<div class="modal fade" tabindex="-1" role="dialog" id="modalmain">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->


JQuery:

$('#modalmain').on('shown.bs.modal', function (e) {
var btn = $(e.relatedTarget);
var auditID = btn.data("data-mylink");
var audittype = btn.data("data-mylink2");
var auditinstanceID = btn.data("data-mylink3");

alert(auditID + " : "+audittype+ " : " + auditinstanceID);

});

Answer

You don't need to include data- in your jQuery since you're already using data.. Try removing the data- like this:

$('#modalmain').on('shown.bs.modal', function (e) {
    var btn = $(e.relatedTarget);
    var auditID = btn.data("mylink");
    var audittype = btn.data("mylink2");
    var auditinstanceID = btn.data("mylink3");

    alert(auditID + " : "+audittype+ " : " + auditinstanceID);
});
Comments