Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not get a simple bootstrap modal to work

I am trying to get a simple bootstrap's modal sample to work, I follow this document which says " You can activate modals on your page easily without having to write a single line of javascript. Just give an element a data-controls-modal attribute which corresponds to a modal element id, ...", but I have done whatever I could and had a huge amount of research still can not get this simple modal to work.

<div class="modal" id="myModal"> <div class="modal-header"> <button class="close" data-dismiss="modal">×</button> <h3>Modal header</h3> </div> <div class="modal-body"> <p>One fine body…</p> </div> <div class="modal-footer"> <a href="#" class="btn">Close</a> <a href="#" class="btn btn-primary">Save changes</a> </div> </div> 

I have either this:

<a class="btn" data-controls-modal="myModal">Launch Modal</a> 

or this:

<a class="btn" data-toggle="modal" href="#myModal" >Launch Modal</a> 

button to activate the modal, I have loaded the bootstrap-modal.js on the page as well. Unless I added a javascript line to handle the "Launch Modal" click event, when I click on "Launch Modal", nothing happens at all, are there anything that I have missed?

like image 839
cnherald Avatar asked May 12 '12 02:05

cnherald


People also ask

How do I manually open Bootstrap modals?

Answer: Use the modal('show') Method You can simply use the modal('show') method to show or open the modal window in Bootstrap using jQuery. Other related Bootstrap's modal methods are modal('hide') and modal('toggle') .

How does Bootstrap modal work?

Modals are built with HTML, CSS, and JavaScript. They're positioned over everything else in the document and remove scroll from the <body> so that modal content scrolls instead. Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.

Do you need jQuery for Bootstrap modal?

If you are using Bootstrap (js), jQuery is required.

Can I use modal without Bootstrap?

You can implement it by using any other library for example Bulma , by using another framework that uses jquery, for example jqueryui, by using any other javascript library that could implement a modal, or by implementing it using javascript, or even with only css and html.


2 Answers

I finally found this solution from "Sven" and solved the problem. what I did was I included "bootstrap.min.js" with:

<script src="bootstrap.min.js"/> 

instead of:

<script src="bootstrap.min.js"></script> 

and it fixed the problem which looked really odd. can anyone explain why?

like image 102
cnherald Avatar answered Sep 30 '22 04:09

cnherald


Fiddle 1: a replica of the modal used on the twitter bootstrap site. (This is the modal that doesn't display by default, but that launches when you click on the demo button.)

http://jsfiddle.net/9RcDN/


Fiddle 2: a replica of the modal described in the bootstrap documentation, but that incorporates the necessary elements to avoid the use of any javascript. Note especially the inclusion of the hide class on #myModal div, and the use of data-dismiss="modal" on the Close button.

http://jsfiddle.net/aPDVM/4/

<a class="btn" data-toggle="modal" href="#myModal">Launch Modal</a>   <div class="modal hide" id="myModal"><!-- note the use of "hide" class -->   <div class="modal-header">     <button class="close" data-dismiss="modal">×</button>     <h3>Modal header</h3>   </div>   <div class="modal-body">     <p>One fine body…</p>   </div>   <div class="modal-footer">     <a href="#" class="btn" data-dismiss="modal">Close</a><!-- note the use of "data-dismiss" -->     <a href="#" class="btn btn-primary">Save changes</a>   </div> </div>​ 

It's also worth noting that the site you are using is running on bootstrap 2.0, while the official twitter bootstrap site is on 2.0.3.

like image 39
mg1075 Avatar answered Sep 30 '22 04:09

mg1075