HTML Dialog Modal Accessible
Use the dialog element for accessible modals with focus trapping and close on backdrop.
Code
<dialog id="modal">
<form method="dialog">
<p>Are you sure?</p>
<button value="cancel">Cancel</button>
<button value="confirm">Confirm</button>
</form>
</dialog>
<button onclick="modal.showModal()">Open</button>
<script>
modal.addEventListener('close', () => {
console.log(modal.returnValue);
});
modal.addEventListener('click', (e) => {
if (e.target === modal) modal.close();
});
</script>Report a problem with this exampleOpens a pre-filled issue on GitHub.