Bootstrap modal not displaying
I've copied and pasted the example code from twitter bootstrap to create a basic modal window in my Rails 3.2 app:
<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
</div>
<a href= "#myModal" role="button" class="btn" data-toggle="modal">Launch demo modal</a>
It's not working. It's not because of jQuery or scripts not loading because a) it doesn't need to load any js because its using data targeting and b) I've checked the console and everything is firing perfectly.
It's also not because I'm including both boostrap.js and bootstrap-modal.js...I've checked and I'm not.
I'm at a loss. It should just work. Other bootstrap js is working fine on the site.
The only thing I can think of is it's something to do with the definition of the classes .hide and .fade - when I take them out, the modal shows up fine. When I include them, it won't show up at all.
Could jQuery UI be interfering with it?
Please ask me for any further info you might need to help me...
UPDATE:
So I think I see the problem, but I don't know how to fix it. When I check the console, right at the top I see that
element.style {
display: none;
}
is somehow now part of the div, so in the console it looks like this:
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;" >
But I don't know why it is adding it in, or where from. How can I track this down?
Thanks
Solution 1:
I tracked down the reason.
Just to give it some general usefulness to anyone coming to this question. If you can't figure out what's wrong, try doing a 'search all' for any classes of 'modal' 'fade' 'fade in' and 'hide' in any style sheets in your application.
I had a single instance of 'fade' being defined in a random css file, and that's what was stopping it displaying as it was overriding bootstrap. Once I deleted the reference, everything was ok.
Solution 2:
If you've upgraded from Bootstrap 2.3.2 to Bootstrap 3 then you'll need to remove the 'hide' class from any of your modal divs.;
Solution 3:
If you're running Bootstrap 4, it may be due to ".fade:not(.show) { opacity: 0 }" in the Bootstrap css and your modal doesn't have class 'show'. And the reason it doesn't have class 'show' may be due to your page not loading jQuery, Popper, and Bootstrap Javascript files.
(The Bootstrap Javascript will add the class 'show' to your dialog automagically.)
Reference: https://getbootstrap.com/docs/4.3/getting-started/introduction/
Solution 4:
I had the same problem. For me the cause was the use of Bootstrap 5 with outdated data-toogle and data-target attributes in the launch button:
After the correction of
<button type="button" class="btn" data-toggle="modal" data-target="#myModal" >
to
<button type="button" class="btn" data-bs-toggle="modal" data-bs-target="#myModal">
it ran correctly.