Skip to content

Instantly share code, notes, and snippets.

@ttamg
Created May 26, 2019 17:58
Show Gist options
  • Save ttamg/01845c258ece4262052d8550492b55ea to your computer and use it in GitHub Desktop.
Save ttamg/01845c258ece4262052d8550492b55ea to your computer and use it in GitHub Desktop.
Opens a modal window and fetches the Django form (in HTML) by AJAX
// Parameters ...
// Trigger: A click on a button with 'openModal' class
// Requires: modal-id as attribute on the button
// URL: fetched from the ajax-url attribute on the modal Content div
$(document).ready(function () {
// A function to run on the click event to open modal, and fetch form via GET AJAX request
$('.openModal').on('click', function () {
var modalID = $(this).attr("modal-id");
var modal = $('#' + modalID);
var modalContent = $('#' + modalID + 'Content');
var modalPlaceholder = $('#' + modalID + 'Placeholder');
var dataURL = modalContent.attr("ajax-url");
$.ajax({
url: dataURL,
type: "GET",
dataType: 'html',
success: function (data, textStatus, XHR) {
modalContent.empty();
modalContent.append(data);
modalContent.find('.submitByAJAX').attr("modal-id", modalID).attr("ajax-url", dataURL); // The POST will need this later
modalPlaceholder.hide();
modal.modal({ show: true });
}
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment