I want to create a Twitter Bootstrap modal, but keep it hidden until I’m ready to show it.
Here is the smallest amount of code to demonstrate. It successfully creates the modal, but it applies display: block to it inline, overriding my style.
modalHtml = '<div class="modal fade" style="display: none" tabindex="-1" role="dialog" id="myModal">'
+ '<div class="modal-dialog modal-lg" role="document">'
+ '<div class="modal-content">Hello World</div></div></div>'
modal = jQuery(modalHtml);
modal.modal('show');
https://jsfiddle.net/Lx23xqbq/2/
I could add this
modal.modal('hide');
But this allows the modal to be visible for a split second.
How can I create a Twitter Bootstrap modal without displaying it to the user?
3
Answers
Modals are hidden via the CSS class
.modal
by default.Remove
modal.modal( 'show' );
.You have
modal.modal( 'show' );
in your JS which causes the modal to be visible for a split second before you callmodal.modal( 'hide' )
.Personally I would place the modal markup into the page before hand.
Injecting into the page wouldn’t be much different.
Technically you are already creating the modal with your first 2 lines of code. It just isn’t displayed until the 3rd line (
modal.modal('show');
).What I think you might be trying to accomplish is separating the code that creates the modal from the code that shows/hides it. The problem (I’m assuming) is that elsewhere in your code you no longer have a reference to the variable
modal
(that you created inmodal = jQuery(modalHtml);
), because it is out of scope.Add this to your HTML page:
Update your Javascript as described below:
https://jsfiddle.net/Lx23xqbq/3/
If I understand your question this have to work:
When page is loaded the function draw() draws the modal and keep it hidden until you click the button, hyperlink, label or whatever you want.