Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 3 - How to load content in modal body via AJAX?

As you can see here, I have a button that launches a modal. Setting an href url for the button this url is automatically loaded into modal by Bootstrap 3. The fact is this page is loaded into modal root (as said in the bootstrap 3 documentation for modals usage). I want to load it into the modal-body instead.

Is there a way to do it via attributes (not javascript)? Or what is the most automatic way to do it?

P.S. I remember in Bootstrap 2 the content was loaded in the body, not the root.

like image 295
Thiezar Avatar asked Oct 29 '13 16:10

Thiezar


People also ask

How can I get dynamic data from bootstrap modal?

Load Dynamic Content from Database in Bootstrap ModalBy clicking the Open Modal ( . openBtn ) button, the dynamic content is loaded from another PHP file ( getContent. php ) based on the ID and shows on the modal popup ( #myModal ).

What is modal in Ajax?

Last updated on February 13, 2022 by Yogesh Singh. Bootstrap Modal is a popup box that is customizable and responsive. It can be used in many different ways – display login or registration form, terms, and conditions, information, image, etc.

Does bootstrap use Ajax?

Bootstrap made it easy to design the feature for desktop, tablet and mobile devices, and Ajax and jQuery made it fast and interactive.


1 Answers

This is actually super simple with just a little bit of added javascript. The link's href is used as the ajax content source. Note that for Bootstrap 3.* we set data-remote="false" to disable the deprecated Bootstrap load function.

JavaScript:

// Fill modal with content from link href $("#myModal").on("show.bs.modal", function(e) {     var link = $(e.relatedTarget);     $(this).find(".modal-body").load(link.attr("href")); }); 

Html (based on the official example):

<!-- Link trigger modal --> <a href="remoteContent.html" data-remote="false" data-toggle="modal" data-target="#myModal" class="btn btn-default">     Launch Modal </a>  <!-- Default bootstrap modal example --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 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="myModalLabel">Modal title</h4>       </div>       <div class="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">Save changes</button>       </div>     </div>   </div> </div> 

Try it yourself: https://jsfiddle.net/ednon5d1/

like image 107
marcovtwout Avatar answered Oct 07 '22 11:10

marcovtwout