Created
May 26, 2019 17:58
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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