Im building an app the hosts restaurant menus. When a menu item is clicked i want to fetch the item's data from my api via ajax and display it on a bootstrap modal.
javascript
(function() {
var infoModal = $('#myModal');
$('.modal-toggle').on('click', function(){
$.ajax({
type: "GET",
url: '/api/menu-item/'+$(this).data('id'),
dataType: 'json',
success: function(data){
var item = JSON.parse(data);
var htmlData = '<ul><li>';
htmlData += item.name;
htmlData += '</li></ul>';
infoModal.find('#modal-body').innerHTML = htmlData;
}
});
infoModal.modal();
});
})(jQuery);
the modal trigger
<a class="modal-toggle" data-toggle="modal" data-target="#myModal" data-id="{{{ $item->id }}}"><h5>{{{ $item->name }}}</h5></a>
the modal
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria- labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<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" id="exampleModalLabel">New message</h4>
</div>
<div class="modal-body" id="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Add to cart</button>
</div>
</div>
the apis response
{"id":4,"menu_category_id":446,"name":"kunzereichert","description":"Dolores impedit ut doloribus et a et aut.","price":"999.99","created_at":"2015-04-10 05:55:23","updated_at":"2015-04-10 05:55:23"}
when i click on the trigger the modal pops up and the ajax request appears on my browsers networks with a status code of 200, however the modals inner html is not updated. I know theres probably docens of these questions here, but i couldnt seem to find one that helped. please help
Im building an app the hosts restaurant menus. When a menu item is clicked i want to fetch the item's data from my api via ajax and display it on a bootstrap modal.
javascript
(function() {
var infoModal = $('#myModal');
$('.modal-toggle').on('click', function(){
$.ajax({
type: "GET",
url: '/api/menu-item/'+$(this).data('id'),
dataType: 'json',
success: function(data){
var item = JSON.parse(data);
var htmlData = '<ul><li>';
htmlData += item.name;
htmlData += '</li></ul>';
infoModal.find('#modal-body').innerHTML = htmlData;
}
});
infoModal.modal();
});
})(jQuery);
the modal trigger
<a class="modal-toggle" data-toggle="modal" data-target="#myModal" data-id="{{{ $item->id }}}"><h5>{{{ $item->name }}}</h5></a>
the modal
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria- labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<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" id="exampleModalLabel">New message</h4>
</div>
<div class="modal-body" id="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Add to cart</button>
</div>
</div>
the apis response
{"id":4,"menu_category_id":446,"name":"kunzereichert","description":"Dolores impedit ut doloribus et a et aut.","price":"999.99","created_at":"2015-04-10 05:55:23","updated_at":"2015-04-10 05:55:23"}
when i click on the trigger the modal pops up and the ajax request appears on my browsers networks with a status code of 200, however the modals inner html is not updated. I know theres probably docens of these questions here, but i couldnt seem to find one that helped. please help
Share Improve this question edited Apr 12, 2015 at 22:18 WaffleBoy asked Apr 12, 2015 at 22:12 WaffleBoyWaffleBoy 511 gold badge1 silver badge3 bronze badges 4- Creating a jsfiddle would simplify the procedure of us helping you. – Amar Syla Commented Apr 12, 2015 at 22:17
- jsfiddle/6o7atqd3 idk if its of much help though, since the ajax call to my server wont work – WaffleBoy Commented Apr 12, 2015 at 22:22
- It's okay, I managed to do it with a fake response. I added an answer below. Please refer to it to fix the problem. – Amar Syla Commented Apr 12, 2015 at 22:26
- Maybe the following link will help you. Bootstrap 3 - How to load content in modal body via AJAX? – Gofoboso Commented Apr 12, 2015 at 22:43
1 Answer
Reset to default 3Instead of this:
infoModal.find('#modal-body').innerHTML = htmlData;
You should use this:
infoModal.find('#modal-body')[0].innerHTML = htmlData;
If you want to stick to all-jQuery way, then use this:
infoModal.find('#modal-body').html(htmlData);
Here's the JSFiddle, using a fake response:
https://jsfiddle/6o7atqd3/1/