Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


How can I make a row clickable in Datatables?

Posted on 2017-08-21
Low Priority
Last Modified: 2017-08-21
I am using jQuery Datables with Ajax, so my rows are created dynamically when the datatable loads. Does anyone know how I can make the row, or a column clickable? I'd like to make it so when a user clicks a row, or one of the items in a column it links them to another page. In the URL they click, I will need to include data from one of the columns in that row. For example, if someone clicked the first row below, I'd want to send them to a url:

ID     Carname    Cartype
77     carA            chevy
76     carB            dodge
Question by:earwig75
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Accepted Solution

Michael Vasilevsky earned 1000 total points
ID: 42263565
Use the mousedown event to open a new window, e.g.:

$( "#myRow" ).on("mousedown", ".clickableRow", function(event) {'' + $(this).attr('id'), '_blank');

Open in new window

Where myRow is the ID of the container holding your rows, clickableRow is a class assigned to all your clickable rows, and 'id' is the id of the row, e.g. 77.

Hope this helps!


Author Comment

ID: 42263570
Note: I ended up using this, but Michael's example is also valid.
$(document).ready(function() {
    var table = $('#example').DataTable();
    $('#example tbody').on('click', 'tr', function () {
        var data = table.row( this ).data();
        alert( 'You clicked on '+data[0]+'\'s row' );
    } );
} );

Open in new window


Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question