3

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">&times;</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

WaffleBoy
  • 51
  • 1
  • 1
  • 3
  • Creating a jsfiddle would simplify the procedure of us helping you. – Amar Syla Apr 12 '15 at 22:17
  • https://jsfiddle.net/6o7atqd3/ idk if its of much help though, since the ajax call to my server wont work – WaffleBoy Apr 12 '15 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 Apr 12 '15 at 22:26
  • Maybe the following link will help you. [Bootstrap 3 - How to load content in modal body via AJAX?](http://stackoverflow.com/questions/19663555/bootstrap-3-how-to-load-content-in-modal-body-via-ajax) – Gofoboso Apr 12 '15 at 22:43

1 Answers1

3

Instead 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.net/6o7atqd3/1/

Amar Syla
  • 3,523
  • 3
  • 29
  • 69
  • thank you so much for your input. Unfortunately this still does not fix my problem :( would you be kind enough to save the working fiddle and give me the link – WaffleBoy Apr 12 '15 at 22:36
  • Then, the problem is at your AJAX call. Giving you the JS Fiddle wouldn't help, because I changed the success to error, because the AJAX call was of course returning an error. If you can upload a live demo of your work, I would help you solve this. – Amar Syla Apr 12 '15 at 22:37
  • I updated the question with the JSFiddle, just in case you want to see what I did. – Amar Syla Apr 12 '15 at 22:38
  • Hi. Instead of AJAX, I am using AngularJS and currently without angular-ui or ui-bootstrap and facing the same problem. Any idea on how I would go about solving this problem with Angular, since accessing DOM elements directly from angular code is a bad practice? – Divij Sehgal Mar 16 '17 at 05:33