Access buttons in alert

Is there a way to access the buttons within an alert programmatically?

e.g. If I’d like to disable the ‘OK’ button until some condition is met, how might I go about it?

1 Like

Excellent question. No, there is no way to access the buttons from code, sadly. But you can do what you want by explicitly removing the standard buttons from the alert (with alert(..., buttons=[]) and then including your own buttons on your custom content form.

You can use the "x-close-alert" event to close the alert and return a particular value when your custom buttons are clicked.

See the documentation of alerts for full details.

Hope that helps!

Marvellous! Thank you.

One further question on this:

Is there some way I can easily style the custom buttons to match those that would have been added by the ‘buttons’ list?

Two options:

  1. Re-show the alert box if the condition isn’t met. If you’re displaying a form in the alert box, this is particularly easy:

  2. Don’t put any buttons on the alert box at all. Use a form that raises the 'x-close-alert' event insead.

You can find examples of both strategies in this app:
https://anvil.works/ide#clone:IGSKDU3E5XTEWABZ=VYHI7YWPSETR3NLOUWFECLFN

Thanks - I’ve been using option 2.

Is there an easy way to style the buttons I’m adding to the form so that they match what would have been added by a normal alert? There seems to be a bit more to it than a simple colour.