I am trying to prettify the confirmation dialog that browser pops when pressing on rails' delete links, with bootbox, so I have modified the delete link's code as follow
<%= link_to '#', :class => "btn btn-danger delete-link", 'data-src' => item_path(@item), :method => :delete do %>
<span class='fa fa-times'></span> Delete IT
<% end %>
$(document).ready(function(){
$('.delete-link[data-method="delete"]').click(function(e){
e.preventDefault();
console.log('THIS SHOULD NOT REDIRECT!');
});
});
But weirdly when I press the button the link actives and deletes the item! but also it logs the THIS SHOULD NOT REDIRECT!
message too!
P.S: I've also tried this
$('.delete-link[data-method="delete"]').unbind('click').click(function(e){...});
but the result is still the same.
Try adding
e.stopPropagation();
so you're code would look like:
$(document).ready(function(){
$('.delete-link[data-method="delete"]').click(function(e){
e.preventDefault();
e.stopPropagation();
console.log('THIS SHOULD NOT REDIRECT!');
});
});
Reference: this stack overflow question has some extra details on these two events.