Close a popup modal - javascript

I have been attempting to get greasemonkey to close a modal by clicking the "x" button. Here is the code for the page.
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true" onClick="window.location.reload()">x</button>
I've tried using document.querySelector('.close').click() but to no avail. Where am I going wrong here?
The page in question is running javascript and ajax.

The code looks like bootstrap modal, so look for the main container ID or class name and use the following code.
$('#modalID').modal('hide');

Why don't you just add the hide class to the modal? That's all that clicking it is going to do.

Related

Closing Bootstrap modal will resize my page

I'm trying to make a bootstrap modal for my page. But whenever I try to close that modal, that modal will resize my entire page.
here is the gif that I took from my page :
https://ibb.co/iN5Bep
also, I use this: http://www.java2s.com/Tutorials/HTML_CSS/Bootstrap_Example/Dialog/Launch_a_modal_dialog_from_a_link_button.htm as my modal
I'm quite new with javascript and bootstrap, and I'm not really sure what to do to fix this. please help
Edit :
i use :
+Add Data
to open :
<div class="bs-example">
<div id="myModal1" class="modal fade">
<div class="modal-dialog" style="width:70%!important;">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">×</button>
<h4 class="modal-title">Add Credentials Data</h4>
</div>
<div class="modal-body">
//contenthere
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
to close it i use :
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">×</button>
or
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
The problem you are facing is: your initial page, without the modal opened, fits in the browser. There is no need of scroll bar. But then you open the modal, and this modal doesn´t fit in the browser, and the scroll bar shows up taking some space. When you close the modal, the scroll bar disappears and everything is back to normal.
You can create a modal that always fits in the window, and allow a scroll bar inside the modal if it's too big.
Or you can save space in case the scroll bar shows up, so your elements page don't get moved around.
You can look for a smaller custom scroll bar
It dependes on requirements and tastes ;)
i solved it by making the body tag position fixed and adding a new div below it.
all thanks to #Ryan Brackett answer from :
Fixed position div scrollable post
Sorry for resuming this old question, but I had the same problem on a legacy project and didn't find the solutions fit my requirements.
The problem is that Bootstrap adds a padding-right: 17px; inline style to the <body> when the modal is shown, to make up for the disappearing scrollbar in browsers where said scrollbar takes up actual space in the viewport (e.g. in all browsers on Windows, for example). But for some reason Bootstrap doesn't seem to take away that padding when the modal is hidden.
I solved this via JavaScript by simply resetting the style property on the modal hidden event:
$("#myModal1").on("hidden.bs.modal", function () {
$('body').css('padding-right', 0);
});
Might not be the cleanest of solutions but it did the job for me, without having to rethink the entire DOM.
I also think this might have been a problem with a particular Bootstrap release, so maybe it cloud have been solved in future updates. But in my case I didn't have time to update the version due to budget constraints.
Hope this helps someone!

Bootstrap Modal not responding

I am using a modal that I used from a previous site where it works fine. Difference is this modal will only appear in IE9 only to direct users to a better browser. I was able to test it with just a alert and works fine.
However, for some reason, when I transfer the code I did for a previous site to this new site, the Modal isn't responding and to one point is not showing on the site.
When I try close the "Close" button, it will just refresh the page. I am not sure why and when I close the "X" button, it does not work either.
The code is inserted in a coldfusion file and I am not sure if it causing conflict or not.
Any help would be appreciated.
Here is what I did for the modal. ie-only detects whether it is IE9 or not:
<!---<div class="ie-only" style="overflow-y:hidden;">
<div class="modal fade in" id="myModal" aria-hidden="false" style="display:block; padding-right:17px;">
<div class="modal-dialog modal-md custom-height-modal">
<div class="modal-content">
<div class="modal-header" style="background-color:##428bca">
<button type="button" class="close" data-miss="modal">x</button>
<h3 class="modal-header" style="background-color:##428bca; text-align:center">Attention</h3>
</div><!---Modal-Header Div--->
<div class="modal-body">
<p style="text-align:center">You are using an outdated browser.<br /> <br /> Upgrade your browser today to better experience this site.</p>
</div><!---Modal-Body Div--->
<div class="modal-footer">
<p class="text-center"><a class="btn btn-default" data-dismiss="modal">Close</a></p>
</div><!---Modal-Footer Div--->
</div><!---Modal-Content Div--->
</div><!---Custom Height Div--->
</div><!---Modal Fade in Div--->
</div><!---Start of Modal Div--->
<!--End of Modal container--> --->
<!---<script>
$(function(){
$('##myModal').modal('show');
}
</script>
<div class="modal-backdrop fade in"></div>--->
It looks like there is a syntax problem in your code:
$(function(){
$('##myModal').modal('show');
}
This is incorrect; it should be:
$(function(){
$('#myModal').modal('show');
});
When corrected the Modal loads as a Modal, which in turn causes the Close button to operate as expected. Absent this the modal still loads (because you have in activated) but loads absent the overlay and absent the hook to Bootstrap's Modal JS which explains why your close button is non-functional.
You can see a functional example here: http://www.bootply.com/24IPYP8O3F
Why your code isn't working
The in class on your Modal div instructs Bootstrap to make that modal visible, and position it according to the Bootstrap CSS. Your jQuery is wrong though, so Bootstrap treats this like a bit of HTML that is just part of the document.
Once you correct your jQuery Bootstrap processes it as expected; as a Modal Javascript component. Once that happens it is presented with the overlay (as default) and the data-dismiss attributes are processed correctly.
The short of it is that the entire issue for why your Modal is not behaving like a Modal is that your jQuery is wrong.

Bootstrap Modal not closing on X or Close button, but does on ESC or clicking in the overlay

Well, the title says it all. I have a default (copied from Bootstrap docs) modal which opens using $('#myModal').modal(); but under no circumstances the modal closes with the dismiss X or cancel button. Console doesn't log any errors. When I press ESC or at any point in the gray transparent overlay it does get closed.
For the sake of being sure I've disabled all other javascript code in my project. I've been staring at this for an hour now. What am I missing? Have done this many times before but I'm baffled with this behavior...
I've just found that with bootstrap 4 if you create a modal with javascript and add it to the document to display it it doesn't set any listener events for the data-dismiss action, so have had to do it myself:
$('#modalId').on('click', 'button.close', function (eventObject) {
$('#modalId').modal('hide');
});
Check if you have data-dismiss attribute as follows
In X icon
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
Close Button
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
If you have them, please copy and paste your code that might help to identify what is causing the problem
Does the close button have data-dismiss="modal" ?
From bootstrap:
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
You could try to toggle or hide manually,
$('#myModal').modal('hide')
Well, after I was sure I'd ruled any external factor out I tried using the bootstrap javascript file from BootstrapCDN.com rather than my pulled in version using composer. You know what? There was a bug in my pulled in version causing this behavior...

BootStrap alerts onclick dissmiss

I am using BootStrap and have an alert for cookies that looks like this:
<div class="alert alert-info fade in">
<button class="close" data-dismiss="alert" aria-label="close" title="close">×</button>
<strong>Info!</strong> This site uses cookies, by using this site you consent to the use of cookies.
</div>
But I want to be able to have an onclick event that does the same as the close button, and after many Googles (and soul searching if it's worth it...) I have come up with nothing.
Can anyone aid me with this? (Either doing in JavaScript/jQuery or HTML is ideal, thanks in advance :))
I cant believe it was this simple in the end... I fixed it with this:
<div class="alert alert-info fade in" data-dismiss="alert" aria-label="close" title="close">
...
</div>
Just copy and paste the data-dismiss etc. into the div element...
You can do achieve it this way --
$('.alert').click(function(){
$('.alert').alert('close')
});
.alert is the class name of the alert box or you can give the alert box an id and use to close it programmatically

Multiple bootstrap modals in a page

I made a bootstrap modal - it's working fine. But when I want to use a modal somewhere else in my site it overwrites the other modal.
Which part of the code do I need to change? And do I have to add like thousands of extra code to javascript for each modal, or can I make a group code?
Thanks!
I added this:
$('#clickme').click(function(e) {
$('#showModal').modal('show');
<script>
function showModal(){
$("#showModal").modal("show");
}
</script>
and:
<div class="modal fade" id="showModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Sign me up!</h4>
</div>
</div>
</div>
</div>
Don't try to do this. Bootstrap's modal is not designed to support that. Its own documentation says
Be sure not to open a modal while another is still visible. Showing more than one modal at a time requires custom code.
It should have said "a lot of custom code". For instance, BS modals use a class on the body element, that is removed when it closed. Therefore, closing a second modal will also hide the first. Then you need special code to detect that a modal was previously open and restore the class on the body. It's much more trouble than it's worth.
Instead, use a "poor man's modal" for the second modal, just an absolutely positioned div you pop up somewhere. You don't need the overlay covering the screen anyway since it's already there from the first modal.

Categories