jQuery v1.11
Given an HTML table with 6 columns, I want the cells in the table in columns two, three, five, and six to respond to click events. Therefore, if the user clicks on a cell in a column of one or four, the click event handler should not be called.
This prevents the event handler from being called when the user clicks in the first column:
$('#my-table').on('click', 'tbody td:not(:first-child)', function (e) { alert("I've been clicked on!"); });
And it prevents the event handler from being called when the user clicks in column 4:
$('#my-table').on('click', 'tbody td:not(:nth-child(4))', function (e) { alert("I've been clicked on!"); });
My question is , how do I change above so that the event handler is not called when a click occurs in a column of one or four.
Jsfiddle
Edit: @micnil answered my specific question and I will find out which template he suggested using. However, @Oleg took the time to point out a better approach. Instead of binding an event handler to each cell, he suggested that I associate an event handler with a table. In my case, it will be better.
Using performance.now() , discussed here , I get the following results setting a binding for jQuery DataTable containing 1000 rows in Chrome:
Binding the click event to cells took 0.14627581768183972 milliseconds. Binding the click event to the table took 0.04619236347855349 milliseconds.
source share