1

I'm using DataTables Select extension so I can select multiple rows. In this jsfiddle example it works, except for the fact that the rows are also selected when an hyperlink is clicked. I want hyperlinks excluded from selecting rows. How can I do this?

This is my DataTables Select initialization:

  $('#example').DataTable({
    select: {
      style: 'multi',
      selector: 'tr:not(a)'
    }
  });
Bob
  • 1,066
  • 2
  • 14
  • 28

2 Answers2

1

I think something like:

$('a.do-nothing').on('click', function(e){
    e.stopPropagation();
});

will do the trick ;)

PS.: I tested in the jsfiddle, so you can use another identifier to the a.do-nothing in your project.

  • Thank you! Just like the other answer from davidkonrad I have a followup question: how do I disable the default behavior as well? In [this example](https://jsfiddle.net/9hhaofky/3/) the links still work (opening in a new page). Just adding `e.preventDefault()`, or stacking it to `e.stopImmediatePropagation()` doesn't work. – Bob Jun 08 '16 at 21:48
1

Simply prevent other listeners to be executed by stopImmediatePropagation() ;

$('a.do-nothing').on('click', function(e){
   e.stopImmediatePropagation();
});

updated fiddle -> https://jsfiddle.net/9hhaofky/2/

preventDefault() as you are using only prevent default behaviour, such as prevent checkbox from being checked.

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • Thank you very much! A followup question though: how do I disable the default behavior as well? In [this example](https://jsfiddle.net/9hhaofky/3/) the links still work (opening in a new page). Just adding `e.preventDefault()`, or stacking it to `e.stopImmediatePropagation()` doesn't work. – Bob Jun 08 '16 at 21:37
  • @Bob - `return false` -> https://jsfiddle.net/9hhaofky/5/ -> [return false from within a jQuery event handler is effectively the same as calling both e.preventDefault and e.stopPropagation on the passed](http://stackoverflow.com/a/1357151/1407478) – davidkonrad Jun 08 '16 at 22:25
  • Thanks again! :) How does it work in this situation though: https://jsfiddle.net/9hhaofky/6/? I can't seem to find the answer to this. :( – Bob Jun 09 '16 at 05:42
  • @Bob, sry - dont quite follow? The last fiddle works for me as well (?). – davidkonrad Jun 09 '16 at 17:15