How do you programmatically open a Bootstrap modal dialog using jQuery? | A1Lab@FahadM to JavaScriptEnglish • 6 days ago

How do you programmatically open a Bootstrap modal dialog using jQuery?

message-square
0

Use $("#myModal").modal("show") in Bootstrap 3 and Bootstrap 4 (jQuery plugin), or bootstrap.Modal.getOrCreateInstance($("#myModal")[0]).show() in Bootstrap 5 (native Bootstrap JS API with a jQuery DOM selector).


1. Bootstrap 3 & 4 (jQuery Plugin)

Call .modal("show") to open the modal and .modal("hide") to close it programmatically.

// Open modal
$("#openModalBtn").on("click", function () {
  $("#infoModal").modal("show");
});

// Close modal
$("#closeModalBtn").on("click", function () {
  $("#infoModal").modal("hide");
});

// Open modal with custom options (static backdrop & disable ESC key)
function openLockedModal() {
  $("#infoModal").modal({
    backdrop: "static",
    keyboard: false,
    show: true
  });
}

// Listen when modal finishes opening
$("#infoModal").on("shown.bs.modal", function () {
  console.log("Modal is now visible (BS 3/4)");
});

2. Bootstrap 5 (Native JS API with jQuery Selector)

In Bootstrap 5, jQuery is not required and $(...).modal(...) does not exist. Pass the raw DOM node ($("#infoModal")[0]) to bootstrap.Modal.getOrCreateInstance(...).

// Open modal in Bootstrap 5
$("#openModalBtn").on("click", function () {
  const modalEl = $("#infoModal")[0];
  if (!modalEl) return;

  const modalInstance = bootstrap.Modal.getOrCreateInstance(modalEl);
  modalInstance.show();
});

// Close modal in Bootstrap 5
$("#closeModalBtn").on("click", function () {
  const modalEl = $("#infoModal")[0];
  if (!modalEl) return;

  const modalInstance = bootstrap.Modal.getOrCreateInstance(modalEl);
  modalInstance.hide();
});

// Open modal with options in Bootstrap 5
function openLockedModalBS5() {
  const modalEl = $("#infoModal")[0];
  if (!modalEl) return;

  const modalInstance = bootstrap.Modal.getOrCreateInstance(modalEl, {
    backdrop: "static",
    keyboard: false
  });
  modalInstance.show();
}

// Listen when modal finishes opening
$("#infoModal").on("shown.bs.modal", function () {
  console.log("Modal is now visible (BS 5)");
});

3. Version-Safe Helper Function in JavaScript

If your code might run against either Bootstrap 4 or Bootstrap 5, check for bootstrap.Modal first and fall back to jQuery .modal(...).

function toggleBootstrapModal(selector, action = "show") {
  const $modal = $(selector);
  if ($modal.length === 0) return;

  if (window.bootstrap && window.bootstrap.Modal) {
    const instance = window.bootstrap.Modal.getOrCreateInstance($modal[0]);
    instance[action]();
  } else if (typeof $modal.modal === "function") {
    $modal.modal(action);
  }
}

// Usage examples
toggleBootstrapModal("#infoModal", "show");
toggleBootstrapModal("#infoModal", "hide");

TL;DR: Use $("#infoModal").modal("show") for Bootstrap 3/4, or bootstrap.Modal.getOrCreateInstance($("#infoModal")[0]).show() for Bootstrap 5.

Which Bootstrap version is your project using, and do you want to populate dynamic text/data inside the modal right before opening it?