I want to add a piece of HTML dynamically to my body, and then use that to show a modal window. The modal.html is an exact copy of the example on the Bootstrap 4 site:
<div class="modal" tabindex="-1" role="dialog" id="dlgModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
I load this using this code:
$('body').append("modal.html");
When I check if the ID exists, I do find an object in the developer's console, but calling modal() on it has no effect:
» $("#dlgModal")
← Object { context: HTMLDocument http://127.0.0.1/index.html, selector: "#dlgModal" }
» $("#dlgModal").modal() /* nothing happens */
How can I load the HTML via Javascript and then call the bootstrap method on it?