Experimentally I've learned that the following code:
<td><%= link_to 'Notify', '/w/'+w.id+'/notify', method: :get, :class => "btn btn-success notify-btn" %></td>
Fires regardless of this jQuery:
$('.notify-btn').click(function(e){
e.preventDefault();
bootbox.confirm("Alert?", function(result){
if(result === true){
return true;
} else {
return false;
}
});
});
And it's due to method: :get. I removed elements of the link one-by-one and tried various combinations. If method: :get is there, it's going to fire.
I can prevent it with e.stopPropagation, but then it won't follow the link at all. Even if I click OK it just sits on the page doing nothing.
If I get rid of the erb and use straight HTML like so:
<a href="/w/53531e8a963e6503c60002b2/notify" class="btn btn-success notify-btn" data-method="get">Notify</a>
And data-method="get" is there, same result. If I remove the data-method, it will pop the alert and prevent following the link, but it won't actually fire the link at all even if I confirm.
Any ideas on what I'm doing wrong?
$('.notify-btn').click(function(e){
e.preventDefault();
e.stopPropagation();
var link = $(this).attr('href')
bootbox.confirm("Alert?", function(result){
if(result){
window.location = link;
return true;
} else {
return false;
}
});
});
I had to use some trickery - but the above works.
Oddly it required both preventDefault and stopPropagation in the end. I tried every possible combination.
I also had the same problem when trying to control if link_to request should be executed or not based on some condition and ended up doing the following
eg assuming classes .link-to and .link-to-with-method
$('.link-to').click(function(){
if(condition){
$('.link-to-get-method').trigger('click');
return true;
}else{
return false;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With