async function accountsDialog() { const div = $(` `); // delete the div when the modal is closed div.on("hidden.bs.modal", function() { div.remove(); }); let accountsList = div.find(".accounts-list"); const accounts = await $.post(`https://${resName}/getAllAccounts`); accountsList.empty(); return new Promise((resolve, reject) => { for(const[accountName, accountLabel] of Object.entries(accounts)) { let accountDiv = $(`
  • ${firstCharToUpperCase(accountLabel)}
  • `); accountDiv.click(function() { div.modal("hide"); resolve(accountName); }); accountsList.append(accountDiv); } div.modal("show"); }); } $(document).on("click", "button[data-dialog-type='account']", async function() { const target = $(this).data("target"); if (!target) return console.log("No target specified"); let targetInput; if (target.startsWith("#")) { targetInput = $(target); } else if (target.startsWith(".")) { const closestContainer = $(this).closest(".dynamic-div"); targetInput = closestContainer.find(target); } if (targetInput.length === 0) return console.log("Target not found"); const result = await accountsDialog(); if (!result) return; targetInput.val(result); });