1

I'm a beginner to coding and I have a project that is using a Bootstrap 4 modal. Currently, the content is inside the HTML page and I want the content to be generated from an external HTML file. Is this possible?

To clarify it further, what I want is put the HTML code of the modal inside a separate HTML file and load the modal content from there with jQuery/JavaScript when needed, instead of putting it inside my index files.

I currently use this code to make the modal.

 if(status){
        $('#mMessage').modal()
    }

and HTML

<div class="modal fade" id="mMessage" tabindex="-1" role="dialog" aria-labelledby="mMessageLabel">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">

        <h4 class="modal-title" id="mMessageLabel">Requirements Not Met</h4>

      </div>
      <div class="modal-body">Please select atleast one.</div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Any assistance you can provide would be greatly appreciated. Thanks.

1

2 Answers 2

3

Do have a look at the JQuery load method.

function myfunction() {
    if(status){
        $('#mMessage').load('./another_page.html');
        $('#mMessage').modal("show");
    }
}

Div with the mMessage id must be in current page. The contents inside will be loaded from different page.

How to handle failed response and how to pass parameters are explained in the JQuery documentation attached above.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. But with this option the modal doesn't close properly.
-2
$('#mMessage').modal("show")

I think This may Help You

Comments

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.