How to select all table columns in a row, but first using jQuery?

I would like to make my row table available. All columns should be interactive, but first. However, I would like to achieve this when they click on a line.

This is the code that I still have:

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

This code is always executed no matter where I click in the line. However, I would like all the cells in the table to be clickable, except for the first.

Is it possible?

+4
source share
3 answers

You can do something like this:

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

    // your code
});

This suggests that if the clicked element is td:first-childor has an ancestor that is td:first-child, do nothing, otherwise continue. "

jsFiddle

+5

CSS Not tr:

$('.table tbody tr:not(:first-child)').click( function (e) {
    alert ($(this).find('td').eq(1).text());
} );
+1

, ,

$('.table tbody tr').delegate( 'td', 'click', function() {
   alert ($(this).text());
    // implement your logic...

});

+1

All Articles