0

My background is in Java and object orientated programming and I'm just starting out in bootstrap. I want to load a modal into another page using javascript and then show it on a button click but I'm not sure of the process. Is this even the correct way to go about this or is html/javascript more procedural?

I want to load the modal in createOrder.html into navbar.html and I have the following. Obviously this doesn't work as I only get a darkened screen when I click the button so what is the correct way to do this?

navbar.html

...
<script> 
$(function() {
$("#createOrder").load("createOrder.html"); 
}); 
</script> 

<button type="button" id="createOrderButton" class="btn btn-default navbar-btn" data-toggle="modal" data-target="#createOrder">Create</button>
...

createOrder.html

<!-- Modal -->
<div class="modal fade" id="createOrder" tabindex="-1" role="dialog" aria-labelledby="createOrderLabel">
  <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">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Create Order</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-primary">Create</button>
      </div>
    </div>
  </div>
</div>

As a side note I understand bootstrap allows you to do $('#myModal').modal(show) but how would I call that modal if the code for it resides on a different page? and if I want to stick to an MVC architecture wouldn't it make more sense to call it via data attributes?

2 Answers 2

2

It looks like you have 2 div tags with the same ID.

You are loading a div#createOrder inserting it to div#createOrder in your navbar.html

The modal data-target is pointing at #createOrder but the javascript is not functioning correctly because of the 2 ID's and is not unhiding the modal content.

The below should fix it.

navbar.html

<div id="createOrderHolder"></div>
<button type="button" id="createOrderButton" class="btn btn-default navbar-btn" data-toggle="modal" data-target="#createOrder">Create</button>
<script> 
$(function() {
     $("#createOrderHolder").load("createOrder.html"); 
}); 
</script> 
Sign up to request clarification or add additional context in comments.

3 Comments

That works, sorta. The modal will pop up but the whole page will be grey out including the modal. I think because the div is showing the modal as well as the button? is there a way to load the createOrder.html into the DOM and then just allow the button to call it without needing a placeholder div?
The placeholder div is only there to hold the loading of the external resource, you can attach it to body if you like. The modal however should be clear and not covered over in grey, if that is happening it could be a separate issue?
Also remove the fade class from the createOrder Div
0

John, I believe you would love this modal plugin as you said you are a java programmer:

http://nakupanda.github.io/bootstrap3-dialog/

By introducing this modal plugin into your project, your 'createOrder.html' should look like:

BootstrapDialog.show({....});

Html markups for creating modals are no longer needed.

1 Comment

Thanks for this, I'll take a look at it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.