user2988394
user2988394

Reputation: 89

How to close the Html5 dialog

At first, I get a dialog in my html

<dialog open>
  <button id="close">Close</button>
</dialog>

And then, I need a JS function to close or hide it.

How to do it ?

Also i have a submit button to show the dialog. How to show it ?

<input type="Submit" value="Submit" id="show" />

Upvotes: 5

Views: 9296

Answers (2)

sdabet
sdabet

Reputation: 18670

The documentation draft for the <dialog> HTML5 tag is available here: http://www.w3.org/html/wg/drafts/html/master/interactive-elements.html#the-dialog-element

Basically you can use the show() and close() methods to interact with the dialog.

Check this tutorial for an example: http://blog.teamtreehouse.com/a-preview-of-the-new-dialog-element

Make sure that you enabled this feature in Chrome:

Once you have Chrome Canary installed go to chrome://flags and enable the Enable experimental Web Platform features flag. You will need to restart the browser for the change to take effect.

Upvotes: 0

Anand Jha
Anand Jha

Reputation: 10714

Try this,

<dialog id="dialog">
  <p>Hi, I'm a dialog!</p>
  <button id="close">Okay</button>
</dialog>

<button id="show">Show Dialog</button>

JS script

var dialog = document.getElementById('dialog');
var showBtn = document.getElementById('show');
var closeBtn = document.getElementById('close');

// Setup an event listener for the show button.
showBtn.addEventListener('click', function(e) {
  e.preventDefault();

  // Show the dialog.
  dialog.show();
});

// Setup an event listener for the close button.
closeBtn.addEventListener('click', function(e) {
  e.preventDefault();

  // Close the dialog.
  dialog.close();
});

for more details see http://blog.teamtreehouse.com/a-preview-of-the-new-dialog-element

Upvotes: 4

Related Questions