JohnDel
JohnDel

Reputation: 2102

Catch rails confirm box action

I have the following rails button:

<%=
  link_to "Remove Product", product_path(product),
    remote: true,
    method: :delete,
    data: { confirm: 'Are you sure you want to delete this product?' }
%>

I'm using rails-ujs to show the confirm() dialog; is there an easy way catch the confirm answer with javascript?

Upvotes: 2

Views: 882

Answers (2)

Martin Tournoij
Martin Tournoij

Reputation: 27822

There is a confirm:complete callback; this is not documented, as far as I can see, I found it by looking at the source.

This gets executed right after the confirm() call:

$(document).on 'confirm:complete', (e, answer) ->
  alert "Your answer was: #{answer}"
  return true # You can still cancel by returning false here

Upvotes: 3

Yahor Zhylinski
Yahor Zhylinski

Reputation: 513

you can handle onclick event and use this:

if (confirm("Say hello?")) {
  alert("hello!");
  ....
} else {
  .....
  alert("another")
}

Upvotes: -1

Related Questions