How to avoid select event when click on a hyperlink

How to avoid select event when click on a hyperlink

peterbelovepeterbelove Posts: 27Questions: 8Answers: 0

I've created this example, the last column is the hyperlink.

I am wondering if there is a way to prevent triggering the "select row event" when I click on the hyperlink.

http://live.datatables.net/gabihiro/2/

thanks,

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Put your link in a < span > inside your < td >, and confine your "select" to the < td >.

            select: {
                style: "os",
                selector: "td"
            }
    

    No doubt there are other ways. This one works for me.

  • peterbelovepeterbelove Posts: 27Questions: 8Answers: 0
    edited January 2021

    I modified my example, but looks like it doesn't work.

    Do you see anything missing here?

    http://live.datatables.net/mefesiwe/1/

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited January 2021

    I should have been clearer. I meant the whole of the link code, that is, your < a > code.

    td span a /a /span /td

  • peterbelovepeterbelove Posts: 27Questions: 8Answers: 0

    you mean something like this? seems like not working.

    <span a target="_blank" rel="noopener noreferrer" href="'+'http://'+data+'">Link</a /span>

    Do you mind edit on my sample?
    http://live.datatables.net/mefesiwe/1/

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    I assumed that you understood HTML.

    <td>
    <span>
    <a ....>....</a>
    </span>
    </td>

  • peterbelovepeterbelove Posts: 27Questions: 8Answers: 0
    edited January 2021

    Not sure why, but still not working. changed to

    <td><span class="weblink"><a target="_blank" rel="noopener noreferrer" href="'+'http://'+data+'">Link</a></span></td>

    http://live.datatables.net/colekaxu/1/edit

    appreciate if you can take another look. thanks,

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Another option is to use select.selector to not use last column:

    selector: 'td:not(:last-child)'
    

    Colin

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    I'm conceding defeat now. I just spotted an instance on my own site where this idea isn't working. I'll have to check out a wider sample of my own coding. Apologies if I wasted your time.
    Use Colin's suggestion, which does work.

  • peterbelovepeterbelove Posts: 27Questions: 8Answers: 0

    anyway, I found this. works perfectly.

    table.on( 'user-select', function ( e, dt, type, cell, originalEvent ) {
    if ( originalEvent.target.nodeName.toLowerCase() === 'a' ) {
    return false;
    }
    } );

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Ah nice, thanks for reporting back,

    Colin

This discussion has been closed.