I link every row in my table with js
<tr class="clickable-row" data-row-id="{{$Offer->offer_nr}}" data-href='link'>
$(".clickable-row").click(function() {
window.location = $(this).data("href");
});
In my last cell, in every row, I have a button to open a modal. How can I cancel the window.location in JavaScript when I click the button to open my modal?
I made the last cell in my row unclickable with CSS:
pointer-events: none;
but the button is then also unclickable
I tried also to stop the window.location when I click on the button with
$(document).on('click', '.delete-record', function (e) {
e.preventDefault();
}
3
Answers
You don’t want to prevent the default action (which is nothing for a click on a table cell), you want to stop the event propagation (jQuery):
This will cause your handler on
.clickable-row
to simply not trigger.You can detect if the button was clicked using the target of what was clicked.
or you can stop the event from going down the tree, but you can not use event delegation on the document because the event has already been fired on the row by the time it gets to the document. So you would need to bind to the buttons directly.
Here’s a possible solution.