Adding on click events for individual rows in a data table( JQuery)

jquery datatables cell click event
datatable row click event
jquery table row click event
jquery datatable button click event
jquery datatable events
jquery datatable pagination click event
datatable row click event not working
jquery datatable pagination click event not working

I am new to javascript, I wanted to create a new application which uses a Data table. The data table is dynamically rendered. I have a set of data which is passed as dataSet.

But the problem is I want to add a function "showDetails(id)" for each row (here id refers to row id). Im stuck with this

$(document).ready(function() {
    $('#example').DataTable( {
    data: dataSet,
    columns: [
        { title: "Name" },
        { title: "Position" },
        { title: "Office" },
        { title: "Extn." },
        { title: "Start date" },
          { title: "Salary" }]
   } );
} );

I would suggest instead of adding showDetails(id) to each row you add it to parent component. In this way only a single click listener will get attached (good from performance perspective). Add onclick to #example element in your case.

How this works is when any of the child element (row) is clicked event will bubble and reach the parent component where you can leverage event.target.id to get hold of which row is clicked and execute appropriate function.

Read more about it here https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_bubbling_and_capture

As written in other post official documentation also suggests it following way where you are adding event listener to tbody and not each row.

$('#example tbody').on('click', 'tr', function () {
    var data = table.row( this ).data();
    alert( 'You clicked on '+data[0]+'\'s row' );
} ); 

DOM / jQuery events, Events assigned to the table can be exceptionally useful for user interaction, however you must be aware that DataTables will add and remove rows from the DOM as This example also uses the DataTables row().data() method to retrieve { var data = table.row( this ).data(); alert( 'You clicked on '+data[0]+'\'s row' ); } ); } );. Onclick event & JQuery – Table row Now that most modern websites use the JQuery library, we should use the correct way of handling click events; This article takes the example of a click triggered onto a row inside a table to highlight the row and load some details inside a field.


after creating your datatable you can simply attach an event to it.

$(document).ready(function() {
     $('#example').DataTable( {
       data: dataSet,
       columns: [
                 { title: "Name" },
                 { title: "Position" },
                 { title: "Office" },
                 { title: "Extn." },
                 { title: "Start date" },
                 { title: "Salary" }]
            });

    $('#example tbody').on('click', 'tr', function () {
      var data = table.row( this ).data();
      alert( 'You clicked on '+data[0]+'\'s row' );
    });
});

for more refrenece you can visit official site example.

jQuery DataTables: Why click event handler does not work , on('click', function(){ // });. However if you go to a different page, sort or filter the table, “Edit” button stops working for those records that were  DOM / jQuery events Events assigned to the table can be exceptionally useful for user interaction, however you must be aware that DataTables will add and remove rows from the DOM as they are needed (i.e. when paging only the visible elements are actually available in the DOM).


Hi the following code will help you to get the row click event in a jQuery data-table, There you can implement the showDetails(id); functionality.

var table = $('#example').DataTable();
          $(document).on("click", "#example tr", function(e) {
                var data = table.row( this ).data();
                alert( 'You clicked on '+data[0]+'\'s row' );
          });

You can look the working fiddle I have created fiddle

DataTables example - Row selection and deletion (single row), DataTables example Row selection and deletion (single row) Also shown is the row().remove()DT method which will delete a row from a table In addition to the above code, the following Javascript library files are loaded for media/js/​jquery. Individual column searching (select inputs) · Highlighting rows and columns  Data can likewise be updated with the row().data() and row().remove() methods. Note that in order to see the new row in the table you must call the draw() method, which is easily done through the chaining that the DataTables API employs.


No matter how the data table is rendered, it still is a markup that is added dynamically to the DOM.

So, a delegated event listener like the following will work.

$("table").on('click', 'tr', function() {
    // Get the id somehow
    let id = 1;
    showDetails(id);
});

DataTables example - Row grouping, A click event handler is added for the grouping rows to allow the grouping order to be restored as well as ordering by any other column. Show. 10, 25, 50, 100. You could add columns property, and on each column you could map your data object by name. And in object data - u get current column object. So here i making link with id from other column (full.memberId). U could create function for button, and use it with OnClick event in button properties.


Jquery datatable add row dynamically, or a 2D Add Columns to Jquery DataTable Dynamically In this example, We are code editor. which will contain all the data of a single column in each row. this What I'm doing is I'm moving a row from one table to another via a click event. jQuery Dynamic Click Event: Bind click event on dynamically added element HTML tags. Here this article explains how to add click event for dynamic added element. In jQuery for click event we can simply use .click() method which works fine, but when you add dynamic HTML and try to achieve click event for it, then there you face problem .i.e .click() is not working.


Event Delegation, In addition, the .trigger() method can trigger both standard browser event names and both the myPlugin and simple namespaces for this particular click event. On a data table with 1,000 rows in its tbody , this example attaches a handler to  It is more about scope. Since a dump of your code would be complicated, I will answer based on my way to code. //myVariables is the namespace that I append to the global scope of JS var myVariables = {}; jQuery.noConflict(); jQuery(document).ready(function() { //you don't need to use var here, because myVariables is in the global scope.


Add, Edit and Delete Data in an HTML Table Using JavaScript and , Add, Edit and Delete Data in an HTML Table Using JavaScript and jQuery. In response to this click event, the productUpdate() function is called, as shown in the Figure 4: Add an Edit button to allow a user to edit a single row in the table. Add table rows on button click and remove the added rows after addition. Enter your data in the input box for each row and add-on button click. After you add the rows, you can remove only those rows which you want to remove from the table. Add and delete rows dynamically and input your data for each row with jQuery.