I'm trying to load the bootstrap modal containing information retrieved from the database.
I'm fetching the results in the modal using AJAX .
The bootstrap modal is loaded correctly but not on the center of the page,it is loaded on the right corner of the page.
I want it to be on the center of the page
I've tried to add css properties on modal class and also search for the problem on different platform but didn't find anything.
After clicking this anchor tag I've bind the modal
<a href="" onclick=" show(<?php echo $ids[$i] ?>)" class = 'fa fa-eye' data-toggle="modal" data-target="#exampleModalLong"'></a>
This is the code for ajax
function show(id)
{
var xhr = new XMLHttpRequest();
xhr.open("GET" ,"findinfo.php?id="+id,true);
xhr.send(null);
xhr.onreadystatechange = function(){
if( (xhr.readyState == '4') && ( xhr.status == '200') )
{
document.getElementById("mbody").innerHTML = xhr.responseText;
}
}
}
here is the modal I've used
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby=" exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="dialog">
<div class="modal-content">
<!-- modal header -->
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<!-- modal body -->
<div class="modal-body" id = 'mbody'>
</div>
<!-- modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
I want The bootstrap modal on the center of the page,it is loaded on the right corner of the page.
Image shows this
Try this,
.modal.fade.show .modal-dialog{
top: 50%;
transform: translateY(-50%);
}
You said in your comment, you have a wrapping container markup:
<div class = 'container'> <div class = 'row'> <div class = col-md-12> Here is my modal... </div> </div> </div>
And thats the problem. The class "col-md-12" uses css "position: relative;" and flexbox stuff. then the "fixed"-position of the modal maybe have some caveats. As I said in my comment you should read the "How it Works"-part.
Solution might be: Place your modal directly within body without any wrapping containers. This should be the preferred way anyhow.
Hope it helps & best regards
Related
For school, I have to make my own website that shows some basic things to first years. So I got the idea, that I make a modal with Bootstrap. If you press the card, the modal will pop up.
Now, I want in the middle where 'CODE WHERE' is, to show the code for something. For example: let me just print or . But it won't let me, it will convert into HTML all the time and I don't want to convert it. I've looked a lot of pages, but I don't seem to find how. Anyone a solution?
Thank you very much.
<!-- Button trigger modal -->
<div style="cursor: pointer;" class="col-md-12" data-toggle="modal" data-target="#exampleModal">
<div class="card mb-4 shadow-sm">
<div class="card-body">
Text
</div>
</div>
</a>
</div>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Code</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<pre>
<code>
CODE HERE??
</code>
</pre>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close Modal</button>
</div>
</div>
</div>
First way is replacing < by < and > by > inside Your <code></code>.
Second way is make it change automatically in javascript:
var codeTags = document.querySelectorAll('code');
for (var i = 0; i < codeTags.length; i++) {
codeTags[i].innerText = codeTags[i].innerHTML;
}
Or easier in jQuery:
$('code').each(function(){
$(this).text($(this).html());
});
I have 2 modals that are triggered by selection of a drop down menu. If I have the "multi-part-delete modal" first and the "multi-part-move" modal second then the "multi-part-move" one does not show. The screen goes dark but the modal does not appear.
If I swap them around then the move one works and the delete one doesn't show. Anyone know how I can get them both to work.
<!-- Multi-Part Delete Modal -->
<div class="modal fade" id="multipartdeletemodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Delete Selected Parts?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<i data-icon="ei-trash" data-size="l" class="trash"></i>
<h5>Are you sure you want to delete these parts?<br>
This process cannot be undone</h5>
<form action="{% url 'multi-delete' project.id %}" method="post" id="multidelete">{% csrf_token %}</form>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button form="multidelete" type="submit" value="Submit" class="btn btn-danger">Delete</button>
</div>
</div>
</div>
</div>
<!-- Multi-Part Move Modal -->
<div class="modal fade" id="multimovemodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Move Selected Parts?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<i data-icon="ei-trash" data-size="l" class="trash"></i>
<h5>Please select group?</h5>
<form action="{% url 'multi-delete' project.id %}" method="post" id="multimove">{% csrf_token %}</form>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button form="multimove" type="submit" value="Submit" class="btn btn-danger">Delete</button>
</div>
</div>
</div>
</div>
use this IIFE function and you will have mulitple stackable modal support on bootstrap 4
(function multiple_modal() {
$(document).on('show.bs.modal', '.modal', function () {
var zIndex = 1040 + (10 * $('.modal:visible').length);
$(this).css('z-index', zIndex);
setTimeout(function () {
$('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack');
}, 0);
});
$(document).on('hidden.bs.modal', '.modal', function () {
$('.modal:visible').length && $(document.body).addClass('modal-open');
});
})()
Yes, stacked modals are currently not working in Bootstrap 4.1. What I did was make them sequentially close/open each other. For example, when a child("stacked") modal opens from a parent modal, the parent modal closes, and when a child modal closes, it re-opens a parent(previously opened) modal - so there's always one modal open at a time.
On the child modal, I have a data attribute data-parent-modal="#parentExampleModal"
I have the Javascript as below:
$('.modal').on('shown.bs.modal', function (e) {
let $this = $(this),
$parentModal = $this.parent().find($this.data('parent-modal') + '.modal');
if ($parentModal.length) {
$parentModal.modal('hide');
$this.on('hidden.bs.modal', function (e) {
$parentModal.modal('show');
});
}
});
If you need further explanation, please let me know.
You could do it with CSS by selecting the second modal
.modal:nth-of-type(even) {z-index: 1052 !important;}
.modal-backdrop.show:nth-of-type(even) {z-index: 1051 !important;}
I have a very basic question. I am developing a login page in .cshtml format. Now I want to implement a modal in bootstrap without using a button. Like if the "condition is wrong" in login credentials then show the modal. I give you the condition below.
technically I see lots of bootstraps available with buttons but I do not want that. I want the modal to execute automatically. or the HTML part of the modal to get triggered without a data target.
#if (ViewContext.ModelState.IsValid == false)//condition for login failure, its in a file called _validation summary.cshtml
{
// Modal html we may change it.
<div class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body…</p>
</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><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
}
Thanks in advance,
Jay
You can use the JQuery to invoke a modal, like
$('#modalId').modal("show");
$('#modalId').modal("hide");
$('#modalId').modal("toggle");
I'm trying to launch a Bootstrap modal without using any buttons or jquery. I would just like show a bootstrap modal when page loads as a information and user should be able to close it using close button or it should close when clicked anywhere on the page. I have tried to use the solution from this SO question
load-without-firing-button-or-using-jquery
However, using the suggested solution prevents the modal from being closed using data-dismiss.
.modal {
display:block;
}
Is there a reason why modal doesn't close in this case ? I have tried applying seperate CSS class but it doesn't make a difference.
.mymodal {
display:block;
}
HTML
<div id="registrationModal" class="modal fade in mymodal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body">
Text
</div>
</div>
</div>
Is it possible to achieve this without using jquery? or valid explanation why such thing is not possible?
The modal doesn't close because there are multiple functions that do different things when you open and close the modal. So when you open the modal the class .in, display block are added and area-hidden="true" is removed from that element. So when you click close there is reverse function. In your case just adding the display block will not work because you need to remove the class .in and remove the whole area-hidden="true" attribute.
Here is the working code:
<style>
.modal{display:block;}
</style>
<script>
$(function() {
$(".modal").addClass("in");
$(".close").click(function(){
$(".modal").removeClass("in").css("display","none").attr("aria-hidden","true");
});
});
</script>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#my-modal">
Launch demo modal
</button>
<div id="my-modal" class="modal fade in">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body">
Hello World!
</div>
</div>
</div>
</div>
<div class="modal-backdrop fade"></div>
Sorry I know you mentioned no jQuery, but there is no CSS fix that I'm aware of, and the jQuery required for this is minimal.
I'd suggest displaying it like this, maybe If no better answer comes up you may consider it:
$('.modal').modal('show');
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div id="registrationModal" class="modal fade in mymodal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body">
Text
</div>
</div>
</div>
I'm making an application form that's on a modal and I made it so the user can upload their picture on the application form. So I have my thumbnail with a "upload picture" bubble inside it, and when I click it, a modal pops up. However the problem is when I click on "cancel" or "upload" on my "upload picture" modal, it closes both modal windows (application form and upload picture).
I'm using bootstrap and here's a snippet of my code:
<div class="modal fade in" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" style="display: block;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Application Form</h4>
</div>
<!-- START OF MODAL BODY-->
<div class="modal-body">
<div class="col-sm-5">
<div class="thumbnail">
<div class="avatar">
<a href="#" class="thumbnail bottom-15" data-toggle="modal" data-target="#upload-avatar">
<img src="img/face1.jpg" alt="...">
</a>
<!-- Upload new avatar bubble -->
<div class="avatar-bubble">
<i class="fa fa-plus"></i> Upload new avatar
</div>
</div>
</div>
</div>
<!--Modal for uploading photo-->
<div class="modal fade" id="upload-avatar" tabindex="-1" role="dialog" aria-labelledby="upload-avatar-title" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="upload-avatar-title">Upload new avatar</h4>
</div>
<div class="modal-body">
<p>Please choose a file to upload. JPG, PNG, GIF only.</p>
<form role="form">
<div class="form-group">
<label for="file">File input</label>
<input type="file" id="file">
<p class="help-block">Files up to 5Mb only.</p>
</div>
<button type="button" class="hl-btn hl-btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="hl-btn hl-btn-green" data-dismiss="modal">Upload</button>
</form>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
</div> <!-- END OF APPLICATION FORM MODAL BODY -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
Basically I put my second modal within my first modal.
Although the Bootstrap 3 docs say "Overlapping modals are not supported..", I think I found a workaround that works. You need to..
Make sure your 2nd modal's markup is outside the body of the 1st modal
Manually close the 2nd modal by using the Bootstrap modal toggle method in jQuery.
Change the z-index of the 2nd modal so that it appears over the first.
http://bootply.com/108243
Also, I changed your markup to remove the fade and `display:none'.
I've opened a modal from another modal. I call it 'modalception'. It sounds like you just need to make sure your events are targeting the correct modal.
I have overlapping modals, and they work as expected. Clicking the background of the second modal closes that modal only, not the one behind it.
Just set the z-index correctly. Be sure not to nest them, and it should work just fine. It is not "supported", but I do it all the time. A project I am currently working on stacks 3 on top of each other with no problems.
I am using Bootstrap v3.3.2 and jQuery 1.11.2.