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


How can I make a row clickable in ajax Datatables?

Posted on 2017-08-21
Low Priority
Last Modified: 2017-08-21
I am using the below code to create an Ajax DataTable. I want to make a row clickable, and have it open a URL using data from one or more of the columns. So, if there is a row with 2 columns, ID and CarType, and that row is clicked, I want the browser to go to the follwing URL when the row is clicked:

This works fine for the first load, but if you perform a search, or the contents dynamically change  and the table is re-drawn, it fails. Can someone please assist?

	function searchCars() {

		// clear the search results
		$("#carsTable tbody").empty();
		// search criteria when form is submitted
		var carType = $("#carType").val();
		var carColor = $("#carColor").val();

		// url to php
		var url = "CarTypes.PHP";

		// argument(s) to add to the url
		url += "&carType=" + carType;
		url += "&carColor=" + carColor;

		var carTable = $('#carsTable').DataTable({
			"order": [
				[ 0, "desc" ]
			"BAutoWidth": true,
			//"stateSave": true,
			"bFilter": false,
			"ajax": url,
			"sAjaxDataProp": "DATA",
			"processing": true,
			// export button 
			"dom": 'Blfrtip',
			"columnDefs": [
				{"visible": false,"targets": [1]}
			buttons: [
			extend: 'collection',
			titleAttr: 'Utility',
			autoClose: true,
			className: 'btn btn-primary btn-xs',
			buttons: [
			aLengthMenu: [
				[5, 25, 100, -1],
				[5, 25, 100, "All"]
		 $('#carsTable tbody').on('click', 'tr', function () {
        		var data = carTable.row( this ).data();
        		alert( 'You clicked on '+data[0]+'\'s row' );
  		  } );

Open in new window

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
  • 3
LVL 59

Expert Comment

ID: 42263785
can you create a simple demo or give us the link to the page

and you should use

$('#carsTable tbody tr').on ("click", function(){

Open in new window

LVL 59

Accepted Solution

HainKurt earned 1000 total points
ID: 42263792
I guess this is what you need

$('#carsTable tbody tr').on ("click", function(){

Open in new window

change that number ":eq(x)" it is 0 based... first td will be 0...

here is a demo

check console, Chrome / F12

Author Comment

ID: 42263803
@HainKurt, I'm sorry, I have a question. I need to use 2 columns, in my row, ID and CarType.
LVL 59

Expert Comment

ID: 42263842
use this to get the 1st td

$('#carsTable tbody tr').on ("click", function(){
  var data =$(this).children(":eq(0)").text()
  alert( 'You clicked on '+data+'\'s row' );

Open in new window


Featured Post

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

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…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

722 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