title: Can javascript listen for the result of a confirm dialogue from a separate method?
body: <p>I know that the usual JS way of taking an action only if a confirmation dialog is to have the confirmation inside the function, like this jQuery example:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    $(function () {
      $("#dropdown").change(function () {
        var success = confirm('Are you sure want to change the Dropdown ????');
  
        if (success == true) {
          // do something                  
        }
        else {
          // Cancel the change event and keep the selected element
        }
      });
    });
  &lt;/script&gt;
  </code></pre>
  
  <p>This doesn't work in my case, because the confirmation is part of a Rails helper:</p>
  
  <pre><code>&lt;%= link_to "Delete", {:controller =&gt; 'foo', :action =&gt; 'delete', :id =&gt; @item.id},
    {:method =&gt; :post, :confirm =&gt; "Are you sure?", :class =&gt; "start_spinner"} %&gt;
  </code></pre>
  
  <p>I want to run some javascript on click (hide the link to prevent clicking twice), but only if the confirmation is true. Unfortunately, at least in Rails 2.3.x, it's not possible to use <code>:onclick</code> when the <code>:method</code> or <code>:confirm</code> options are present. (This behavior is noted <a href="http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to#955--confirm-popup-and-method-override-onclick" rel="nofollow">on ApiDock for the link_to helper</a> and is the same in my application.)</p>
  
  <p><strong>EDIT:</strong>
  I think I created some confusion by including the example from ApiDock instead of my exact code. Sorry about that. I've modified the code below to reflect the behavior that Rails has when the <code>:method =&gt; :post</code> option is included, as it must be in my code. The difference between the two outputs is still just dropping the <code>destroyJsFunction</code> method, but there's a whole bunch of other stuff that does happen in the <code>onclick</code> that seems to interfere with implementing the solutions suggested by Sukima and agnoster.</p>
  
  <pre><code>&lt;%= link_to "Delete", {:controller =&gt; 'foo', :action =&gt; 'delete', :id =&gt; @item.id}, :method =&gt; :post, :confirm=&gt;"Are you sure?", :onclick=&gt;"destroyJsFunction()", :class =&gt; "start_spinner" %&gt;
  # expected output
  # =&gt; &lt;a href="/foo/delete/1" class='start_spinner' onclick="if (confirm('Are you sure?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', 'CSRF_TOKEN'); f.appendChild(s);f.submit(); }; {destroyJsFunction()}; return false;"&gt;Delete&lt;/a&gt;
  # actual output
  # =&gt; &lt;a href="/foo/delete/1" class='start_spinner' onclick="if (confirm('Are you sure?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', 'CSRF_TOKEN'); f.appendChild(s);f.submit(); }; return false;"&gt;Delete&lt;/a&gt;
  </code></pre>
  
  <p><strong>END EDIT</strong></p>
  
  <p>Because I'm using Rails to submit the <code>POST</code> request, I can't just re-write the link in plain HTML. </p>
  
  <p>I'd like to run my javascript on click of a link with the class <code>start_spinner</code>, but I can't find a way to re-display the link if the confirmation is false. Is there a way to have a separate javascript listen for the results of a confirmation dialogue?</p>