29

I have a page of links to internal pages I need to open up in a Bootstrap modal DIV. The problem is that it seems that using the latest version of Bootstrap v3 in conjunction with jQuery v2.1.4 just doesn't work when it comes to loading content this way. There's plenty of tutorials I've read about creating modals with Bootstrap and how remote content is being phased out. But there's got to be away to make this work with jQuery, or maybe not.

The theory is that when you click

<a class="" href="/log/viewslim?id=72" title="View" data-target="#myModal" data-toggle="modal">View 72</a>

the content of data-load-remote is supposed to be read and injected into the div with class modal-body.

<div id="myModal" class="modal fade">
<div class="modal-dialog">
    <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Event</h4>
            </div>
            <div class="modal-body">
                <p>Loading...</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Save changes</button>
            </div>
    </div>
</div>

However, when I try this example with jQuery v2.1.4 and BS v3.3+, what it does is open up a modal window with grey background but all the styling of the modal window is gone. Meaning it seems to only display the modal-body div, but the modal header, pretty modal frame and bottom buttons in modal-footer div are not displayed at all. The only way to close the box is to click outside the modal box.partial modal window

I've found examples all around about how to open up remote urls this way, but they all use outdated version of bootstrap, not the version I'm working with. Can anyone shed some lights on this please?

2
  • Examples that I've found that basically do what I want, but these are done with older versions of BS/jQ: http://plnkr.co/edit/HWSgSw and http://jsfiddle.net/sherbrow/thlyb/
    – vanarie
    Dec 29, 2015 at 2:47
  • 1
    $('#myModal').on('show.bs.modal', function (e) { $(this).find('.modal-body').load('/log/viewslim?id=72'); }); works fine if you have a static url to load, but what if someone needs to pull the url from a link instead?
    – vanarie
    Dec 29, 2015 at 13:13

6 Answers 6

52

So basically, in jquery what we can do is to load href attribute using the load function. This way we can use the url in <a> tag and load that in modal-body.

<a  href='/site/login' class='ls-modal'>Login</a>

//JS script
$('.ls-modal').on('click', function(e){
  e.preventDefault();
  $('#myModal').modal('show').find('.modal-body').load($(this).attr('href'));
});
1
  • Removing all references to BS's magic method attributes (data-target, data-toggle, etc.) from the <a href> tag was what I needed to do to make this work. If you're NOT needing to load a remote URL into the modal, then magic attributes work fine. Thanks!
    – vanarie
    Dec 29, 2015 at 16:47
17

From Bootstrap's docs about the remote option;

This option is deprecated since v3.3.0 and has been removed in v4. We recommend instead using client-side templating or a data binding framework, or calling jQuery.load yourself.

If a remote URL is provided, content will be loaded one time via jQuery's load method and injected into the .modal-content div. If you're using the data-api, you may alternatively use the href attribute to specify the remote source. An example of this is shown below:

<a data-toggle="modal" href="remote.html" data-target="#modal">Click me</a>

That's the .modal-content div, not .modal-body. If you want to put content inside .modal-body then you need to do that with custom javascript.

So I would call jQuery.load programmatically, meaning you can keep the functionality of the dismiss and/or other buttons as required.

To do this you could use a data tag with the URL from the button that opens the modal, and use the show.bs.modal event to load content into the .modal-body div.

HTML Link/Button

<a href="#" data-toggle="modal" data-load-url="remote.html" data-target="#myModal">Click me</a>

jQuery

$('#myModal').on('show.bs.modal', function (e) {
    var loadurl = $(e.relatedTarget).data('load-url');
    $(this).find('.modal-body').load(loadurl);
});
8
  • Sorry, I forgot to mention that I already read several times that it's depreciated as of v3.3.0, but I can't get it to work with jQuery and I'm not a javascript guru. I've tried a lot of things, but nothing seems to work that I've tried so far. If it's possible to have JQ do the work of opening the modal and injecting the remote url, I can't figure out how to get that part to work.
    – vanarie
    Dec 29, 2015 at 3:02
  • @vanarie OK, updated my answer with some custom jQuery.
    – worldofjr
    Dec 29, 2015 at 3:09
  • I verified that the .on() is firing when the modal is opened, and it displays the entire modal window, css styling and all, but the .load never injects the remote content into .modal-body. So at present, it either works but looks like my images, or it loads properly but never injects the remote. Maybe it's not finding .modal-body inside the main modal outer divs?
    – vanarie
    Dec 29, 2015 at 3:33
  • Ok, this does work the way expected. However, when I add the url as <a data-toggle="modal" href="remote.html" data-target="#modal">Click me</a>, I can't pull the url into the .load statement for each l link.
    – vanarie
    Dec 29, 2015 at 4:05
  • 1
    Thank you for your help sir! I got it working before you made your last edits, but you're right on target too. Hopefully this will help someone else with the same issues.
    – vanarie
    Dec 29, 2015 at 16:59
1

A different perspective to the same problem away from Javascript and using php:

<a data-toggle="modal" href="#myModal">LINK</a>

<div class="modal fade" tabindex="-1" aria-labelledby="gridSystemModalLabel" id="myModal" role="dialog" style="max-width: 90%;">
    <div class="modal-dialog" style="text-align: left;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Title</h4>
            </div>
            <div class="modal-body">
                <?php include( 'remotefile.php'); ?>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

and put in the remote.php file your basic html source.

1

e.relatedTarget.data('load-url'); won't work
use dataset.loadUrl

$('#myModal').on('show.bs.modal', function (e) {
    var loadurl = e.relatedTarget.dataset.loadUrl;
    $(this).find('.modal-body').load(loadurl);
});
1
  • This is not an answer to the question, it should have been a comment on another answer.
    – worldofjr
    Sep 24, 2017 at 9:42
0

If using @worldofjr answer in jQuery you are getting error:

e.relatedTarget.data is not a function

you should use:

$('#myModal').on('show.bs.modal', function (e) {
    var loadurl = $(e.relatedTarget).data('load-url');
    $(this).find('.modal-body').load(loadurl);
});

Not that e.relatedTarget if wrapped by $(..)

I was getting the error in latest Bootstrap 3 and after using this method it's working without any problem.

0

In bootstrap-3.3.7.js you will see the following code.

if (this.options.remote) {
  this.$element
    .find('.modal-content')
    .load(this.options.remote, $.proxy(function () {
      this.$element.trigger('loaded.bs.modal')
    }, this))
}

So the bootstrap is going to replace the remote content into <div class="modal-content"> element. This is the default behavior by framework. So the problem is in your remote content itself, it should contain <div class="modal-header">, <div class="modal-body">, <div class="modal-footer"> by design.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.