As mentioned in the comments/anwers here, it is possible to generate this event using a MutationObserver().
As the answers here seem cruelly incomplete (even wrong), I did my own experiments to compose an easily exploitable answer.
Nota :
_ <dialog>
JS objects always have an open
property, and it is a boolean value (true or false), even if this attribute is not present on the HTML declaration.
1st solution :
(() => // IIFE => add event 'dialog-open' to all Dialog elements
{
const
OpenEvent = new CustomEvent('dialog-open')
, ObserverM = new MutationObserver( recs =>
{
recs.forEach( ({attributeName: attr, target: dial }) =>
{
if (attr === 'open' && dial.open )
dial.dispatchEvent( OpenEvent );
})
});
document.querySelectorAll('dialog').forEach( dial =>
{
ObserverM.observe( dial, { attributes: true } );
})
})() // IIFE end...
const
btShowDial = document.querySelector('#bt-show_mDialog')
, dialogBox = document.querySelector('#dialog-box')
, dialBForm = document.querySelector('#dialog-box form')
;
btShowDial.onclick =_=>
{
dialogBox.showModal()
}
dialogBox.addEventListener('dialog-open', () =>
{
dialBForm.inNum.valueAsNumber = 0 | Math.random() *10**4
dialogBox.returnValue = 'exit with Escape key'
})
dialogBox.onclose=_=>
{
console.log(`${dialBForm.inNum.value } is ${dialogBox.returnValue}` )
}
<button id="bt-show_mDialog">show modal dialog..</button>
<dialog id="dialog-box">
<form method="dialog">
<h3>Dialog -1-</h3>
<label> mumeric : <input type="number" name="inNum"></label>
<br>
<button value="good"> good </button>
<button value="bad" > bad </button>
</form>
</dialog>
Personally, I prefer to use their event methods directly on DOM Objects.
They have the advantage of being unique, and of making the code more readable.
so might as well add this one, rather than creating a specific custom event!
2nd solution :
(() => // IIFE => add event 'dialog-open' to all Dialog elements
{
const Dialogs = document.querySelectorAll('dialog');
Dialogs.forEach( dial => dial.onOpen = function(){} ) // add onOpen empty method.
const ObserverM = new MutationObserver( recs =>
{
recs.forEach( ({attributeName: attr, target: dial }) =>
{
if (attr === 'open' && dial.open ) dial.onOpen(); // call onOpen Method...
})
});
Dialogs.forEach( dial => ObserverM.observe( dial, { attributes: true }))
})() // IIFE end...
const
btShowDial = document.querySelector('#bt-show_mDialog')
, dialogBox = document.querySelector('#dialog-box')
, dialBForm = document.querySelector('#dialog-box form')
;
btShowDial.onclick =_=>
{
dialogBox.showModal()
}
dialogBox.onOpen =_=>
{
dialBForm.inNum.valueAsNumber = 0 | Math.random() *10**4
dialogBox.returnValue = 'exit with Escape key'
}
dialogBox.onclose=_=>
{
console.log(`${dialBForm.inNum.value } is ${dialogBox.returnValue}` )
}
dialog::backdrop {
background: #00ddff5d;
}
<button id="bt-show_mDialog">show modal dialog..</button>
<dialog id="dialog-box">
<form method="dialog">
<h3>Dialog -1-</h3>
<label> mumeric : <input type="number" name="inNum"></label>
<br>
<button value="good"> good </button>
<button value="bad" > bad </button>
</form>
</dialog>