Destory CSS Bootstrap Modal - javascript

I have a list of data in a table and a button next to each row. When I click the buton, the modal loads up a relevant remote URL into the modal.
My submit button in said modal has the following jQuery attatched:
$('#editTrackModal').modal('hide');
When the modal hides, and I load up another remote URL into the modal, the modal appears, but on slower internet connections, the original content of the modal stays there for sometimes 4-5 seconds before being replaced.
Essentially, rather than hiding, I want to 'hide and destroy' the modal, and then re-create it.
Is this possible?
Simply destroying the modal after pressing the button closes the window, but then does not allow a subsequent modal to re-open until the page has reloaded.

You could clean the content of the modal, before show, I dont know how you load the content, you could use something like this:
$("#editTrackModal .modal-body").empty();

Hi you can do like this
// the modal is distroyed on hide event..
$('#editTrackModal').on('hidden', function(){
$(this).data('modal', null); //this will destroy you data and model means it will reset.
});
and it may be duplicate of how to destroy bootstrap modal window completely?
you can refer to that link also.

A good way to ensure that the modal's content is clean after hiding/closing it is can be done like this:
$('#editTrackModal').on('hidden.bs.modal', function () {
$('#editTrackModal div').remove(); //When the hidden event is triggered, remove all the contents of the modal.
});
And when you want to show the modal again, you can do something like this:
$('#editTrackModal').append($('.modal-contents')); //create first the contents of the modal, then attach it.
$('#editTrackModal').modal('show');
Your html may look like this:
<div id="editTrackModal" class="modal fade">
<div class='modal-contents'>
......Build your modal contents here......
</div>
</div>

Related

Bootstrap modal close through js bug

How do I close bootstap modal programmatically? I checked so many answers about it, but none of them work... All below methods close modal, but keeps backdrop of it so I tried to remove backdrop with $('.modal-backdrop').remove(); it breaks modal and if I show it after removing backdrop scroll doesn't work on modal. I need to close it through js and whenever I want to by calling method. I know I can use data-dismiss="modal" to close it.
how I'm hiding modal:
$('#mymodal').modal("toggle");
$('#mymodal').modal("hide");
$('.close').click();
Update
So I see that for a lot of people it works with $('#mymodal').modal("hide"); but when I Inspect page I can see that there is two modal-backdrops in the body and after I call hide it closes modal and first modal-backdrop, but second one stays and this is why it's not working.
with modal open:
After closing modal with $('#mymodal').modal("hide");
This should work. if not you have other problem in code. Read modal javascript docs https://getbootstrap.com/docs/4.0/components/modal/#via-javascript
$('#myModal').modal('hide')

addthis share button not working when I open the modal twice

I'm using addthis share button in my website, but the click of the button is in a modal that is loaded dynamically, so when the modal html is loaded, I call the script of addthis this way:
var addthisScript = document.createElement('script');
addthisScript.id = 'addthisscript';
addthisScript.setAttribute('src',
'//s7.addthis.com/js/300/addthis_widget.js#domready=1')
document.body.appendChild(addthisScript);
So, when I open the modal at first time, it works!
But, if I close the modal and open again and click the button to share again, it just reload the page.
After reloaded the page, starts to work again, but just once.
I already try remove the script from DOM and create again, but still doesn't work!!
When the modal is closed, I remove the entire HTML, this way:
$(`#${id_modal}`).on("hidden.bs.modal",() => {
$(`#${id_modal}`).remove();
}
And every time that I open the modal, I call the function addthis() that create the script above.
Any help?

Close modal inline dialog by clicking on button in sub-region and prevent it from re-opening

I've created a region, let's call it Notifications with Static ID: P1_NOTIFICATIONS, set its Template to Inline Dialog and added a sub-region called row-01 without any Static ID.
row-01 has a button OK with P1_MODAL_OK ID and the following Behavior: Action: Redirect to URL, Target: javascript:apex.navigation.modal.close(true,["P1_NOTIFICATIONS"]) or Target: javascript:apex.navigation.modal.close(true).
Neither JS API calls seem to work, modal doesn't close on clicking my button.
Tried also a dynamic action to Hide the P1_NOTIFICATIONS region, but it only hides its content, doesn't close the modal itself with all the overlays and stuff. The header and empty body are still visible.
Also there's an [x] Close button in the header, but I need to hide it. I also can't find its action, the script it calls to close the modal inline dialog region.
How to close the modal from my P1_MODAL_OK custom button? And how to catch the action triggered by the [x] button from modals header?
EDIT 1: openModal('P1_NOTIFICATIONS') works as well as closeModal('P1_NOTIFICATIONS'). The second solves my problem.
To close modal inline dialog region, create a Redirect to URL button with the following call in target: javascript:closeModal('P1_NOTIFICATIONS'), where P1_NOTIFICATIONS is region's Static ID.
What worked for me, i Created a dynamic action on the button with the action Close Region and then under affected element i selected the region for the inline dialog

Foundation reveal-modal: how can I return to parent modal after closing modal-in-a-modal

Here is an example of modal in a modal.
http://foundation.zurb.com/docs/components/reveal.html
Is there any way to return to the first modal in the same state, after closing child one?
Or, even better, how can I get modal over modal, not replace first modal with second?
Update: OK, I can just open first modal again, state is the same.
$('#parent-dialog').foundation('reveal','open');
Is it possible to make modal-over-modal?
Is this what you're looking for?
You can create a reveal modal with another inside it. Setting
reveal.multiple_opened to true will not close previously opened reveal
modals. You can even put a video into a reveal.
Source: http://foundation.zurb.com/sites/docs/v/5.5.3/components/reveal.html

Making jquery Dialog working between two pages

I have a dialog setup as follow
$("#myDialog").dialog({
autoOpen: true,
close: function(event, ui) {
$("#myDialog-content").html("");
$(this).dialog("destroy");
}
});
$("#myDialog").css("min-height","");
$("#myDialog-content").html("Loading...");
$.ajax({
...
success: function(response) {
$("#myDialog-content").html(response);
}
});
This working fine I load and close dialog in same page but not able to make it work properly where I move between pages.
Here is a my page flow
From source page(say PageA) I make AJAX call to load the page containing dialog div(say PageB).
Link on this page call above method to display dialog. (For first time it runs OK).
When I click close button. Dialog close and with firebug I can still see dialog div at the end with UI classes but in hidden state.
If I go back to source page (Page A) and reload the PageB.In firebug I can see two div - one originally from JSP and second one from step 3.
Now if I click button to load dialog box - It used hidden to populate new data and never use new div created by jquery. So I just have blank dialog box.
I am not sure if this is jquery Dialog issue or my page flow. One possible solution I though of is use remove in close function to remove dialog div completely but it puts burden to create this div everytime page PageB is loaded. Is there any other way or any thing I am doing wrong in this scenario?
If i understood correctly the situation, You have 2 options:
If you somehow cleaning the content of "Page B", remove the modal
then.
If you do not have the cleaning mechanism like that, just
.remove() content of modal on close
Sidenote: i would advise not to use jquery for .css and .html('Loading...'). Also, it is good to cache jquery elements in variables e.g var dialog = $("#myDialog");

Categories