0

I have a table where each <tr> links to another page. This is code i use to archive this.

Table:

<tbody>
    <tr class="td-link" data-href="/invoice/preview/207/">
         <td>#14000059</td>
         <td>
             <a class="kt_ajax_link" data-invoiceid="207" data-csrf-token="43e848a61f3dadedc182a1454c8070550e4f275fe83d53ef1ac9f961281f1ba4" data-action="/invoice/delete/"><i class="fa fa-trash-alt"></i></a>
        </td>
    </tr>                                                                                                                                            
</tbody>

jQuery

$(".td-link").click(function() {
    window.location = $(this).data("href");
});

How can i make the link <a> work inside the table, without triggering the jQuery link?

I have tried with z-index, but cant get it working.

Any suggestions?

Jan
  • 2,853
  • 2
  • 21
  • 26
puntable
  • 299
  • 6
  • 19

2 Answers2

2

You can prevent the click event from propagating up to the <tr/> by adding this attribute to the <a> tag: onclick="event.stopPropagation();".

More info here: https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation

edit: didn't mean to steal Mohamad's comment. Will delete if he posts his as an answer

wrymug
  • 186
  • 1
  • 8
1
  • first you have to replace this line :

    $(this).data("href"); by this line :

    $(this).attr("href");

    • Second, it is better to work with this :

      $("body").on('click','.td-link',function(e) {..} instead of $(".td-link").click(..)

    • Finally, you can do the work as @rosslh & @Mohammed suggest to you .

here is an example that I tested and it worked for me :

$("body").on('click','.td-link',function(e) {
    //e.preventDefault();
    //e.stopPropagation();
    var $link = $(this).attr("data-href");
    window.location = $link;
  });
sohaieb azaiez
  • 768
  • 9
  • 20