Alexander Hristov
Alexander Hristov

Reputation: 311

How to get the ID of the last open bootstrap modal dialog in jQuery?

I want to load the ID of the latest modal dialog I've opened inside a hidden field. How can I do this? Here's my function:

$(document).on('show.bs.modal', '#modalFileUpload, #modalFileUpload, #SadlModal, #PlanModal, #StatusModal', function () {
    $('#hid_ActiveDialogID').val('IDOFCURRENTLYOPENMODALHERE');
    $(this).keyup(function (e) {
        var modalFooter = $("#" + $('#hid_ActiveDialogID').val());
        var btnSave = $("#" + $('#hid_ActiveDialogID').val() + " div.modal-footer").children().first();

        if (e.which === 13) //enter
        {
            e.preventDefault();
            if (modalFooter.is(":visible")) {
                modalFooter.children().first().focus();
                btnSave.trigger('click');
            }
        }
    });
});

This is the specific line of code I'm targeting with this question:

$('#hid_ActiveDialogID').val('IDOFCURRENTLYOPENMODALHERE');

Upvotes: 1

Views: 2485

Answers (1)

N. Ivanov
N. Ivanov

Reputation: 1823

To get the currently active modal (assuming it's one of the id's on line 1 of your snippet), just use:

$('#hid_ActiveDialogID').val($(this).attr("id"));

Hope this helps!

Upvotes: 1

Related Questions