Wait for alert confirmation to run Stimulus code


#1

I wanted to run some stimulus code when I delete an element in my DOM.
But I also wanted to get a confirmation from the user, thanks to a native alert (“Are you sure you want to delete this element?”).
But It seems the Stimulus code is triggered before my alert acceptance.
Is there any way to trigger it after?
Thank you


#2

Sounds like what you want is Window.confirm, not Window.alert.

MDN: https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm


#3

Sorry. I made a mistake in my first. It is a confirm.
But I wanted to use the confirm: option in rails link_to.
So I can’t precisely control the flow.
The confirm will have to be in the stimulus controller instead.
Thank you.