Having trouble with dismissing an alert

What I’m trying to do:
I am trying to dismiss an alert from the code when a user triggers an action, but I am not able to do so.

What I’ve tried and what’s not working:
I have tried triggering a close event, but I am not able to get the text box to close.
I am triggering the close event when the user hits the enter key on their keyboard.
Code Sample:

from ._anvil_designer import Form1Template
from anvil import *
import anvil
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
import time
class Form1(Form1Template):

    def __init__(self, **properties):
        # Set Form properties and Data Bindings.

        # Any code you write here will run when the form opens.
        self.a = None

    def close_box(self, **event_args):
        """This method is called when the button is clicked"""
        print(self.a.raise_event("x-close-alert", value=42))

    def keypress(self, which, key):
        keydict = {
        except Exception as e:

    def button_1_click(self, **event_args):
        """This method is called when the button is clicked"""
        self.a = alert("this is an alert",dismissible=True)

Javascript for triggering the keypress function

$(document).on('keypress', (e) => {
  anvil.call($('.content'), 'keypress', e.which, e.key)

Clone link:

Thank you so much for all of your help!

The alert function returns a bool value. When you execute self.a.raise_event, you’re calling raise_event on a bool.

The docs in the x-close-alert section say To close the alert from code inside your form. You have to be using a form as custom content for the alert to raise the x-close-alert event on that custom form.

All of this should work fine if you put your code inside a custom form and use that form inside the alert. You’d want to use the Anvil/Javascript bridge to set the keydown handler inside the custom form. e.g. use anvil.js.get_dom_node and then use addEventListener on that. You shouldn’t need any actual Javascript, it’ll all work from Python.


Thank you so much for your help!!

1 Like

would you be so kind as to post the solution as a clone-able Anvil ?

Generally speaking, rather than resurrecting already solved posts you should post a new question with details on what you’ve tried and what you’re having issues with.

However, since this would help future people who find this topic, I’ll post my standard forum example for using alerts with custom content: Anvil | Login