JavaScript alert with 3 buttons

Nowdays, you can use an HTML dialog element.

<dialog open>
  <p>Greetings, one and all!</p>
  <button>Ok</button><button>Maybe</button><button>Cancel</button>
</dialog>

And do what you want with it.

document.querySelectorAll('button').forEach($button => 
  $button.onclick = () => document.querySelector('dialog').removeAttribute('open'))

https://jsfiddle.net/6nus2zt4/1/

Hope this help to future generations :)


Unfortunately, I don't think you can change the number of buttons in an alert box. (At least not in any browser that I know of.) You can use one of the many modal dialog scripts that are out on the net. Something like Eric Martin's SimpleModal Dialog for jquery would probably work.

They basically take a div and style it up with css and javascript to mimic a dialog box.


however you can use confirm function.. but NOT three buttons.

var r=confirm("Press a button!");
if (r==true)
  {
  x="You pressed OK!";
  }
 else
  {
  x="You pressed Cancel!";
  }

No.

You need to use a custom modal dialog, such as jQuery UI Dialog.

Tags:

Javascript