Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select the all table columns in a row but the first with jQuery?

I would like to make my table row clickable. All of the columns need to be clickable but the first one. However I would like to achieve this when they are clicking on the row.

This is the code I have so far:

    $('.table tbody tr').click( function (e) {
        alert ($(this).find('td').eq(1).text());
    } );

This code executes always no matter where I click in the row. However I'd like that all of the table cells should be clickable except the first one.

Is it possibe?

like image 325
Radical_Activity Avatar asked Mar 21 '23 19:03

Radical_Activity


2 Answers

You could do something like this:

$('.table tbody tr').on('click', function(e) {
    if ($(e.target).closest('td:first-child').length) {
        return;
    }

    // your code
});

This says "if the clicked element is a td:first-child or has an ancestor that is a td:first-child, do nothing; otherwise continue."

jsFiddle

like image 88
lonesomeday Avatar answered Apr 25 '23 16:04

lonesomeday


Use the CSS Not selector and skip first tr element:

$('.table tbody tr:not(:first-child)').click( function (e) {
    alert ($(this).find('td').eq(1).text());
} );
like image 43
juju Avatar answered Apr 25 '23 15:04

juju