Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!doctype html>
<html lang="en">

<body>
  <h1>Hello, world!</h1>

  <!-- Button trigger modal -->
  <button type="button" class="btn btn-primary btn-modal" data-target-modal-id="id_modal1">
    Launch modal1
  </button>

  <!-- Modal1 -->
  <div class="modal fade" id="id_modal1" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="id_modal1_title">Modal-1</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <p id="id_modal1_body" style="text-align:center;">Modal Body</p>
          <div style="text-align:right;">
            <button type="button" class="btn btn-primary btn-modal" id="id_modal1_btn" data-target-modal-id="id_modal2">Launch modal2</button>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- Modal2 -->
  <div class="modal fade" id="id_modal2" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="id_modal2_title">Modal-2</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <p id="id_modal2_body" style="text-align:center;">Modal Body</p>
          <div style="text-align:right;">
            <button class="btn btn-success" id="id_modal2_btn" data-bs-dismiss="modal">close</button>
          </div>
        </div>
      </div>
    </div>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
</body>

</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                // 現在表示中のModalインスタンスを取得する
function getCurrentModal() {
  const modal_elm = document.querySelector(".modal.show");
  if (!modal_elm) {
    return null;
  }
  return bootstrap.Modal.getInstance(modal_elm);
}

// 一度だけ実行するイベントハンドラを設定する
function setOneShotEventHandler(target, eventName, handler) {
  // 実行後に自身を解除するイベントハンドラを作成
  const oneShotHandler = function (e) {
    // 自身を解除
    target.removeEventListener(eventName, oneShotHandler);
    // 本来実行したかった関数を実行
    handler(e);
  };
  target.addEventListener(eventName, oneShotHandler);
}

// 指定したIDのModalを表示し、指定した初期化関数を実行する
function startModal(modal_id, initFunc, ...params) {
  // Modal要素が存在しなかったら何もしない
  const modal_elm = document.getElementById(modal_id);
  if (!modal_elm) {
    return;
  }
  // 表示対象のModalを生成・取得する
  const modal = bootstrap.Modal.getOrCreateInstance(modal_elm);
  // 対象のModalが表示された時に、初期化関数を実行する
  setOneShotEventHandler(modal._element, "shown.bs.modal", function () {
    if (initFunc) {
      initFunc(...params);
    }
  });
  // 現在表示中のModalを取得
  const currentModal = getCurrentModal();
  if (currentModal) {
    // 現在表示中のModalがある場合
    // イベントハンドラを設定する
    setOneShotEventHandler(
      currentModal._element,
      "hidden.bs.modal",
      function () {
        // 本来表示したいModalを表示する
        modal.show();
      }
    );
    setOneShotEventHandler(modal._element, "hidden.bs.modal", function () {
      // もともと表示されていたModalを表示する
      currentModal.show();
    });
    // 現在表示中のModalを非表示にして、イベントの連鎖を開始
    currentModal.hide();
  } else {
    // 現在表示中のModalがない場合
    // Modalを表示する
    modal.show();
  }
}

document.addEventListener("DOMContentLoaded", function () {
  document.querySelectorAll(".btn-modal").forEach((elm) => {
    elm.addEventListener("click", function (e) {
      e.preventDefault();
      const modal_id = e.target.dataset.targetModalId;
      startModal(modal_id);
    });
  });
});

              
            
!
999px

Console