The native dialog element

A built-in modal with focus handling and Escape to close.

HTML · The native dialog elementIntermediate
<button type="button" id="open">Open</button>

<dialog id="dlg" aria-labelledby="dlg-title">
  <h2 id="dlg-title">Are you sure?</h2>
  <form method="dialog">
    <button type="submit" value="cancel">Cancel</button>
    <button type="submit" value="ok">OK</button>
  </form>
</dialog>

<script>
  document.getElementById('open').addEventListener('click', function () {
    document.getElementById('dlg').showModal();
  });
</script>
dialogmodalshowModalclose